gpt4 book ai didi

Android fragment 重新实例化错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:31 27 4
gpt4 key购买 nike

在我旋转屏幕之前,我认为我已经很好地完成了对 fragment 的转换。我收到以下类型的错误:

RuntimeException:无法启动 Activity ComponentInfo{com.ghcssoftware.gedstar/com.ghcssoftware.gedstar.GedStar}:android.support.v4.app.Fragment$InstantiationException:无法实例化 fragment com.ghcssoftware.gedstar.PersonTab$ PersonTabFrag:确保类名存在,是公共(public)的,并且有一个公共(public)的空构造函数

有问题的类确实存在,是公共(public)的,我添加了一个空的构造函数,结果没有变化。查看一些示例代码,我确实注意到与我的代码编写方式的一些差异,尽管我也没有看到空的构造函数:

1) 是否有任何理由像许多样本一样将我的 fragment 类声明为“静态”?

2) 我是否需要在 fragment 类中实现“newInstance”的使用?为什么这样做而不是仅仅有一个构造函数?例如来自 V14 样本之一:

public static class CountingFragment extends Fragment {
int mNum;

/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static CountingFragment newInstance(int num) {
CountingFragment f = new CountingFragment();

// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);

return f;
}

我对一些 Java 概念还是有点不熟悉,所以这里可能缺少一些基本的东西。

道格·戈登 GHCS 软件

最佳答案

Is there any reason that my fragment class should be declared "static" as many samples are?

仅当它是某物的内部类时。由于您的似乎是 PersonTab 的内部类,因此它需要是静态的。或者,将它移到 PersonTab 之外,成为一个独立的 Java 类。

Do I need to implement the use of "newInstance" within my fragment class? Why is this done instead of just having a constructor?

它只是一个工厂方法。框架不需要它。

关于Android fragment 重新实例化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8275571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com