片段真的需要一個空的構造函數嗎? - Do fragments really need an empty constructor?

問題:

I have a Fragment with a constructor that takes multiple arguments.我有一個帶有多個參數的構造函數的Fragment My app worked fine during development, but in production my users sometimes see this crash:我的應用程序在開發過程中運行良好,但在生產中,我的用戶有時會看到此崩潰:

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

I could make an empty constructor as this error message suggests, but that doesn't make sense to me since then I would have to call a separate method to finish setting up the Fragment .正如此錯誤消息所暗示的那樣,我可以創建一個空的構造函數,但這對我來說沒有意義,因爲從那時起我將不得不調用一個單獨的方法來完成Fragment設置。

I'm curious as to why this crash only happens occasionally.我很好奇爲什麼這種崩潰只是偶爾發生。 Maybe I'm using the ViewPager incorrectly?也許我錯誤地使用了ViewPager I instantiate all the Fragment s myself and save them in a list inside the Activity .我自己實例化了所有Fragment並將它們保存在Activity內的列表中。 I don't use FragmentManager transactions, since the ViewPager examples I have seen did not require it and everything seemed to be working during development.我不使用FragmentManager事務,因爲我看到的ViewPager示例不需要它,並且在開發過程中一切似乎都在工作。


解決方案:

參考一: https://stackoom.com/question/hqc0
參考二: Do fragments really need an empty constructor?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章