android - 无法实例化 fragment

标签 android android-fragments

Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public

是不是因为我的 Fragment 不是静态类? 是不是因为我的 Fragment 是一个内部类?

如果我将 Fragment 设为静态类,我对 findViewById 的所有引用都会失败,这意味着需要进行大量重构。

如何在不将内部 Fragment 变为静态类的情况下解决此问题?

最佳答案

is it because my Fragment is an inner class

如果您的 fragment 是内部类,则它必须是静态内部类。理想情况下,它是一个独立的公共(public) Java 类。

if I make my Fragment a static class, all my references to findViewById fail, which means a LOT of refactoring

无论如何,您都需要进行重构。小部件现在由 fragment 拥有,而不是 Activity 。 fragment 应该尽可能少地了解包含它们的 Activity ,以便它们可以根据需要在不同的 Activity 之间移动以支持手机、平板电脑、电视等。

How can I solve this without turning my inner Fragment into a static class??

你让它成为一个独立的公共(public) Java 类。

关于android - 无法实例化 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7016632/

相关文章:

android - 如果从 Home 或 Background 打开,应用程序不会恢复并再次从 Main Activity 启动

java - 如何以编程方式从 Android 中的 DatePicker/TimePicker Spinner 中提取值?

java - Android-几天后运行后出现代码错误

android - Android.mk 中的预处理器宏被忽略,但在 Application.mk 中有效

android - Cordova 地理定位不适用于 Android

安卓软键盘不会消失

Android 动画矢量 Drawable : change rotation degree at runtime

android - 从 getItemPosition 更新 ViewPager 中的 fragment

java - fragment 中是否有类似 setResult() 的方法?

android - 在单个 fragment 中处理后退按钮