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/