gpt4 book ai didi

java - 退出 Fragment 后在 onSaveInstanceState 中抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 11:16:41 26 4
gpt4 key购买 nike

我有一个名为 HomeActivityActivity,它使用 fragment HomeFragmentOwnershipFragment

当我单击 HomeFragment 中的 ListView 项时,将执行 fragment 事务并启动 OwnershipFragment

OwnershipFragment 中,我有以下 onSaveInstanceState 方法:

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

for(int i = 0; i <mDirectors.size(); i++)
mDirectors.get(i).updateFromForm();

outState.putSerializable(DIRECTORS, mDirectors);
}

然后在 onCreateView...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

final View rootView = inflater.inflate(R.layout.fragment_ownership, container, false);
mFormContainer = (LinearLayout) rootView.findViewById(R.id.ownership_form_container);

mManager = new SessionManager(getActivity());
mDirectors = new ArrayList<Director>();

if (savedInstanceState != null)
mDirectors = (ArrayList<Director>) savedInstanceState.getSerializable(DIRECTORS);
else
mDirectors = mManager.getDirectors();

//Other stuff

return rootView;

}

基本上,应用程序会在以下情况后崩溃:

  1. 我单击打开 OwnershipFragment 的 ListView 项。 (工作正常)
  2. 我按下后退按钮,这会启动一个返回到 HomeFragment 并将 OwnershipFragment 放入返回堆栈的事务。 (工作正常)
  3. 我转动手机以将方向更改为横向模式。 (工作正常)
  4. 我将它调回纵向,应用程序崩溃并在以下位置给出了 NullPointerException:

for(int i = 0; i < mDirectors.size(); i++) //in the onSaveInstanceState method

我不明白为什么,因为我找不到为 mDirectors 分配 null 值的位置。我不确定要提供什么其他代码,所以请告诉我,我会把它放进去。

如果当前显示的 fragment 是 HomeFragment,为什么首先调用 OwnershipFragmentonSaveInstanceState/p>

这是我的 Logcat:

07-15 11:28:24.440: E/AndroidRuntime(12150): FATAL EXCEPTION: main
07-15 11:28:24.440: E/AndroidRuntime(12150): java.lang.NullPointerException
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.opendoors.core.OwnershipFragment.onSaveInstanceState(OwnershipFragment.java:204)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Fragment.performSaveInstanceState(Fragment.java:1840)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1598)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1655)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Activity.onSaveInstanceState(Activity.java:1233)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:546)
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.opendoors.core.HomeActivity.onSaveInstanceState(HomeActivity.java:220)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Activity.performSaveInstanceState(Activity.java:1181)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1233)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3778)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread.access$800(ActivityThread.java:152)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1288)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.os.Looper.loop(Looper.java:137)
07-15 11:28:24.440: E/AndroidRuntime(12150): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-15 11:28:24.440: E/AndroidRuntime(12150): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 11:28:24.440: E/AndroidRuntime(12150): at java.lang.reflect.Method.invoke(Method.java:511)
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-15 11:28:24.440: E/AndroidRuntime(12150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-15 11:28:24.440: E/AndroidRuntime(12150): at dalvik.system.NativeStart.main(Native Method)

最佳答案

根据您的评论,更改您对 mDirectors 的定义到

ArrayList<Director> mDirectors = new ArrayList<Director>();

或者在你的 for as 中检查 null

for(int i = 0; mDirectors != null && i < mDirectors.size(); i++)

关于java - 退出 Fragment 后在 onSaveInstanceState 中抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24751071/

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