Android兼容包Fragment作为内部静态类
我一直在使用 Android 兼容性包,但遇到了以下问题,似乎每当我在应用程序上创建一个片段作为内部静态类并尝试启动该活动时,它都会显示以下错误
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment org.wr.CreditCardHolderActivity.CreditCardHolderFragment: make sure class name exists, is public, and has an empty constructor that is public
并且当我分离片段时而且活动一切顺利,有人知道为什么吗?我该如何解决它?
谢谢!
I've been using the Android compatibility Package but i encountered the following issue, it seems that whenever i create a Fragment as an inner static class on my application and try to start that activity the it display the following error
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment org.wr.CreditCardHolderActivity.CreditCardHolderFragment: make sure class name exists, is public, and has an empty constructor that is public
And when i separate the fragment and the activity everything work smoothly, anyone know why? and how can i fix it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个内部类 Fragment,例如:
确保您的限定符是 public 当 FragmentActivity 尝试重新启动片段时,它不会从具体类中调用它,它将从抽象 FragmentActivity 中处理它,并且如果您的内部类 Fragment 是私有的,则 Activity 不会引用 onSaveState、onRestoreState、initialise 等。
private
到public
为我修复了它!更新:
查看https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/Fragment.java
当尝试从保存状态(片段被完全破坏)恢复片段时,
instantiate
方法会调用newInstance()
。newInstance
方法要求该类可公开访问,因此当定义为内部类时,这意味着它必须在适当的情况下为public
和static
。希望这能解决一些未来的问题。
If you have an inner class Fragment like:
Make sure your qualifier is public when the FragmentActivity tries to reinitiate the fragment it doesn't call it from the concrete class it will handle it from the abstract FragmentActivity, and if your inner class Fragment is private the Activity has no reference to onSaveState, onRestoreState, initialise etc..
private
topublic
fixed it for me!Update:
Have a look at https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/Fragment.java
The
instantiate
method callsnewInstance()
when trying to restore theFragment
from a saved state (where the fragment was completely destroyed).The
newInstance
method requires the class to be publicly accessible, so when defined as an inner class this means it has to bepublic
andstatic
where appropriate.Hope this clears up some future questions.