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的原因之一。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,349评论 25 708
  • Fragment是什么? Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏...
    luoqiang108阅读 428评论 0 1
  • 堂哥是2012年结的婚,那年他29岁。 29是周岁,依我们老家虚岁算已经30岁了。 30岁的年纪看起来也不算晚,但...
    张看简书阅读 267评论 0 3
  • 秋渐凉, 繁星两三点, 月圆无所见; 南飞雁, 看尽人间事, 何处是故乡? 落叶纷纷离家去, 细雨绵绵无绝期; 虽...
    滴缘阅读 159评论 0 0
  • 今天不写读书分享,原因是假期对书籍的选择有限,加之最近也没有阅读到什么很有营养让人精力充沛很振奋人心的书籍...
    Julia的随笔阅读 242评论 0 0