2

我有一個Activity調用HomeActivity,它使用片段HomeFragmentOwnershipFragment退出片段後拋出onSaveInstanceState的NullPointerException

當我單擊HomeFragment中的listview項時,將執行片段事務並啓動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的列表視圖項上。 (正常工作)
  2. 我按下後退按鈕,後退按鈕開始一個事務,返回到HomeFragment並將OwnershipFragment放在後退堆棧上。 (正常工作)
  3. 我打開手機將方向更改爲橫向模式。 (正常工作)
  4. 我把它回縱向和應用程序崩潰,並給出了在NullPointerException

對(INT I = 0;我< mDirectors.size();我++)//在方法的onSaveInstanceState

爲什麼,但因爲我找不到在哪裏mDirectors被分配一個null值我想不通。我不知道還有什麼其他的代碼來提供,所以才讓我知道,我把它英寸

爲什麼叫擺在首位的OwnershipFragmentonSaveInstanceState如果顯示的當前片段是HomeFragment

這裏是我的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) 
+0

其中一個方法返回null if(savedIns tanceState!= null) mDirectors =(ArrayList )savedInstanceState.getSerializable(DIRECTORS); else mDirectors = mManager.getDirectors();' –

+0

我在這些語句之前放置了一個斷點,並且在錯誤發生時它們沒有被調用。當我改變'HomeFragment'的方向時,我不認爲'OwnershipFragment'的onCreateView'甚至會被調用。 – ujvl

回答

1

根據您的意見,您的mDirectors定義修改爲

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

或檢查空你的作爲

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

如果當前片段是HomeFragment,爲什麼首先調用OwnershipFragment的'onSaveInstanceState'? – ujvl

+0

無法從您發佈的代碼中分辨出來。 –

+0

是否有助於發佈「HomeFragment」代碼和/或「HomeActivity」代碼? – ujvl

相關問題