FragmentDialog异常 InstantiationException

Buggly 统计出FragmentDialog 报错

android.support.v4.app.Fragment$InstantiationException:   
Unable to instantiate fragment 省略:  
make sure class name exists, is public, and has an empty constructor that is  
public  

说FragmentDialog必须要有公开的无参构造器。

目测应该是初始化FragmentDialog的时候报错。
除了我们自己代码创建FragmentDialog之外,系统也会帮我们创建FragmentDialog。这也是FragmentDialog与Dialog的最大区别!

Default constructor.  Every fragment must have an
empty constructor, so it can be instantiated when restoring its
activity's state.  It is strongly recommended that subclasses do not
have other constructors with parameters, since these constructors
will not be called when the fragment is re-instantiated; instead,
arguments can be supplied by the caller with setArguments()
and later retrieved by the Fragment with getArguments().

所有的Fragment必须有无参的构造器,这样在Activity恢复的时候,会去重新初始化Fragment。这个初始化操作,就是通过反射到无参构造器进行初始化的。

还有一点需要注意:FragmentDialog 传参的时候,应该像Fragment一样,通过setArguments()、getArguments() 进行传参。

这也是出现Bug的原因之一。

    原文作者:wan7451
    原文地址: https://www.jianshu.com/p/2f71108e8de2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞