片段真的需要一个空的构造函数吗? - 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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章