Unable to instantiate fragment 问题解决

最近友盟log报了一个fragment的异常,这个异常其实实属罕见,也不容易遇到
《Unable to instantiate fragment 问题解决》 WechatIMG17.jpeg

注意到Unable to instantiate fragment com.wonderfull.mobileshop.g.a.a: could not find Fragment constructor,是说fragment不能被是实例化,那么问题来了,为什么不能被实例化呢,首先,fragment什么时候会被实例化,当然是我们在代码中去add或者replace这个fragment的时候,除此以为还有什么地方去实例化fragment吗,有,宿主Activity被销毁后重新恢复的时候,它的fragment也会被恢复,进行重新实例化,这是由系统来完成的,通过反射机制。
好,说到这,去瞅瞅源码,我们看看fragment的构造函数:

/**
     * Default constructor.  <strong>Every</strong> 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 {@link #setArguments}
     * and later retrieved by the Fragment with {@link #getArguments}.
     *
     * <p>Applications should generally not implement a constructor. Prefer
     * {@link #onAttach(Context)} instead. It is the first place application code can run where
     * the fragment is ready to be used - the point where the fragment is actually associated with
     * its context. Some applications may also want to implement {@link #onInflate} to retrieve
     * attributes from a layout resource, although note this happens when the fragment is attached.
     */
    public Fragment() {
    }

仔细看函数注解,我粗略翻译一下:

强烈推荐fragment的继承类不要去实现带参的构造函数,因为这些带参构造函数在fragment被再次实例化的时候将不会被调用,那么这些参数也就丢失,建议通过setArguments方式进行参数传递。”

其实这段话还有一层意思,既然在再次实例化的时候不会调用我们声明的带参构造函数,那么必然调用了无参构造函数,问题来了,如果你满足了下面两个条件:

  1. 为自己的fragment添加了带有参数的构造器
  2. 没有额外声明一个无参的构造函数,或者声明了,但是是private的

那么就悲催了,因为你无意间覆盖了无参构造函数,是的,这样就会报上面那个异常,通过检查代码,确实犯了这个问题,莫名其妙的声明了一个private类型的fragmeng空构造器,那么问题也就不可避免了,之所以说这个问题难遇到,是因为我们平常确实很少去为fragment声明其他的构造器,但问题出了就要找到原因。

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