Fragment com.example.myapplication.PersonalCenter must be a public static class to be properly recreated from instance state.
时间: 2024-05-19 07:16:07 浏览: 10
As an AI language model, I can explain that this error message indicates that the "PersonalCenter" fragment class in the "myapplication" package needs to be declared as a public static class. This is required for the fragment to be properly recreated from its saved instance state. When a fragment is recreated, its state is restored from the saved instance state, and if the fragment class is not declared as a public static class, it may not be possible to restore its state correctly, leading to unexpected behavior or crashes. Therefore, it is important to ensure that fragment classes are declared as public static classes to avoid this error.
相关问题
Fragment null must be a public static class to be properly recreated from instance state.
这个错误通常出现在 Android 应用程序中的 Fragment 类中,表示当您尝试恢复 Fragment 时出现问题。这个错误的原因可能是当您的 Fragment 被销毁并重新创建时,它的构造函数中的参数也被重新创建了,然而从保存的实例状态中恢复 Fragment 时却无法使用这些参数。
要解决这个问题,您需要确保您的 Fragment 是一个公共静态类,这样它就可以正确地从实例状态中重新创建。您可以将 Fragment 类定义为公共静态类,并且不要将构造函数中的任何参数存储为实例变量,而应该将它们作为 Fragment 参数传递给 Fragment 的 newInstance() 方法。这样,当您尝试恢复 Fragment 时,FragmentManager 可以使用 Fragment 的无参数构造函数重新创建 Fragment 实例,并使用 newInstance() 方法中传递的参数进行初始化。
下面是一个示例代码:
```
public class MyFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public MyFragment() {
// Required empty public constructor
}
public static MyFragment newInstance(String param1, String param2) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my, container, false);
}
}
```
在这个示例中,我们将 Fragment 定义为公共静态类,并将任何参数存储为 Fragment 参数,而不是实例变量。这样就可以正确地从实例状态中恢复 Fragment。
java.lang.IllegalStateException: Fragment null must be a public static class to be properly recreated from instance state.
这是一个关于 Android Fragment 的异常,它通常发生在 Fragment 类没有被定义为公共静态类的情况下。具体来说,如果在 Fragment 生命周期的某个阶段中,系统尝试从其实例状态中重建 Fragment 实例,并且实例不是公共静态类,则会触发此异常。如果您遇到此问题,您可以将 Fragment 定义为公共静态类或使用 Fragment 参数化构造函数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)