記一次Could not find Fragment constructor

本次排查Bug的fragment基于AndroidX1.1.0
線上bugly報(bào)了一個(gè)Could not find Fragment constructor跪妥,先看一下Fragment的構(gòu)造函數(shù)

/**
     * Constructor used by the default {@link FragmentFactory}. You must
     * {@link FragmentManager#setFragmentFactory(FragmentFactory) set a custom FragmentFactory}
     * if you want to use a non-default constructor to ensure that your constructor
     * is called when the fragment is re-instantiated.
     *
     * <p>It is strongly recommended to supply arguments with {@link #setArguments}
     * and later retrieved by the Fragment with {@link #getArguments}. These arguments
     * are automatically saved and restored alongside the Fragment.
     *
     * <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() {
        initLifecycle();
    }

主要看一下注釋說明激挪,如果你想要的使用非默認(rèn)的構(gòu)造函數(shù)碧信,需要自己實(shí)現(xiàn)一個(gè)FragmentFactory去初始化垫卤,然后強(qiáng)烈推薦使用setArguments和getArguments存取參數(shù)。看了一下報(bào)錯(cuò)的類,可以基本確定是使用了帶參數(shù)的構(gòu)造函數(shù)

然后看一下報(bào)錯(cuò)的位置,在Fragment里搜索could not find Fragment constructor

 public static Fragment instantiate(@NonNull Context context, @NonNull String fname,
            @Nullable Bundle args) {
        try {
            Class<? extends Fragment> clazz = FragmentFactory.loadFragmentClass(
                    context.getClassLoader(), fname);
            Fragment f = clazz.getConstructor().newInstance();
            if (args != null) {
                args.setClassLoader(f.getClass().getClassLoader());
                f.setArguments(args);
            }
            return f;
        } catch (java.lang.InstantiationException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        } catch (IllegalAccessException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        } catch (NoSuchMethodException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": could not find Fragment constructor", e);
        } catch (InvocationTargetException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": calling Fragment constructor caused an exception", e);
        }
    }

可以看到這里通過反射层玲,調(diào)用了空的構(gòu)造函數(shù)

Fragment f = clazz.getConstructor().newInstance();

但是對(duì)應(yīng)的類沒有空的構(gòu)造函數(shù),所以拋出了這個(gè)異常反症。然后看一下初始的地方辛块,是FragmentActivity在onCreate中調(diào)用的mFragments.restoreSaveState(p);

結(jié)論:
Fragment必須有一個(gè)無參public的構(gòu)造函數(shù),否則在數(shù)據(jù)恢復(fù)的時(shí)候铅碍,會(huì)報(bào)crash
ps:手動(dòng)測(cè)試發(fā)現(xiàn)私有的構(gòu)造函數(shù)無法通過該Constructor.newInstance方法調(diào)用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末润绵,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子胞谈,更是在濱河造成了極大的恐慌尘盼,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件烦绳,死亡現(xiàn)場(chǎng)離奇詭異卿捎,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)径密,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門午阵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人享扔,你說我怎么就攤上這事底桂。” “怎么了惧眠?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵籽懦,是天一觀的道長。 經(jīng)常有香客問我氛魁,道長暮顺,這世上最難降的妖魔是什么厅篓? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮拖云,結(jié)果婚禮上贷笛,老公的妹妹穿的比我還像新娘应又。我一直安慰自己宙项,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布株扛。 她就那樣靜靜地躺著尤筐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪洞就。 梳的紋絲不亂的頭發(fā)上盆繁,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音旬蟋,去河邊找鬼油昂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛倾贰,可吹牛的內(nèi)容都是我干的冕碟。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼匆浙,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼安寺!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起首尼,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤挑庶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后软能,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迎捺,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年查排,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了凳枝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡雹嗦,死狀恐怖范舀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情了罪,我是刑警寧澤锭环,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站泊藕,受9級(jí)特大地震影響辅辩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一玫锋、第九天 我趴在偏房一處隱蔽的房頂上張望蛾茉。 院中可真熱鬧,春花似錦撩鹿、人聲如沸谦炬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽键思。三九已至,卻和暖如春甫贯,著一層夾襖步出監(jiān)牢的瞬間吼鳞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來泰國打工叫搁, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赔桌,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓渴逻,卻偏偏與公主長得像疾党,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子裸卫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355