2017-08-24 288 views
0

專家,片段時啓動另一片段

我有以下問題掙扎:我有一個開始片段A.片段A的活動開始片段B點擊一個按鈕時。片段B有一個ListView和一個自定義的BaseAdapter來擴展它。

當我在後臺放置應用程序的同時顯示片段B時,我在onSaveInstanceState()的片段A中得到以下異常。

任何幫助非常感謝!謝謝!

// BaseActivity addFragment() 
public void addFragment(@IdRes int id, Fragment fragment) { 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(id, fragment, fragment.toString()); 
    fragmentTransaction.addToBackStack(fragment.toString()); 
    fragmentTransaction.commit(); 
} 

// Start Fragment A 
BaseActivity activity = (BaseActivity) getActivity(); 
activity.addFragment(R.id.container, new FragmentAFragment()); 

// In FragmentAFragment: Show Fragment B 
BaseActivity activity = (BaseActivity) getActivity(); 
activity.addFragment(R.id.container, FragmentBFragment.newInstance(getString(R.string.input_list), selectedListItem, new FragmentAFragment.ListSelectorListener() { 
    @Override 
    public void onSelected(final String selected) { 
     // handle selection 
    } 
})); 

FATAL EXCEPTION: main 
android.support.v4.app.FragmentState.writeToParcel(Fragment.java:148) 
    at android.os.Parcel.writeTypedArray(Parcel.java:1306) 
    at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:564) 
    at android.os.Parcel.writeParcelable(Parcel.java:1495) 
    at android.os.Parcel.writeValue(Parcel.java:1401) 
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:723) 
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 
    at android.os.Bundle.writeToParcel(Bundle.java:1133) 
    at android.os.Parcel.writeBundle(Parcel.java:763) 
    at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3603) 
    at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3744) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Caused by: java.io.NotSerializableException: com.company.myapp.FragmentA$1 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224) 
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218) 
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) 
    at android.os.Parcel.writeSerializable(Parcel.java:1521) 
    at android.os.Parcel.writeValue(Parcel.java:1474)  
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:723)  
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)  
    at android.os.Bundle.writeToParcel(Bundle.java:1133)  
    at android.os.Parcel.writeBundle(Parcel.java:763)  
    at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:148)  
    at android.os.Parcel.writeTypedArray(Parcel.java:1306)  
    at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:564)  
    at android.os.Parcel.writeParcelable(Parcel.java:1495)  
    at android.os.Parcel.writeValue(Parcel.java:1401)  
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:723)  
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)  
    at android.os.Bundle.writeToParcel(Bundle.java:1133)  
    at android.os.Parcel.writeBundle(Parcel.java:763)  
    at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3603)  
    at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3744)  
    at android.os.Handler.handleCallback(Handler.java:751)  
    at android.os.Handler.dispatchMessage(Handler.java:95)  
    at android.os.Looper.loop(Looper.java:154)  
    at android.app.ActivityThread.main(ActivityThread.java:6077)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  
+1

的[java.io.NotSerializableException]可能的複製(https://stackoverflow.com/questions/13895867/java-io-notserializableexception) – sushildlh

+0

如果你有一個的onSaveInstanceState'()''在已覆寫FragmentA'那麼你也應該添加該代碼。同樣採取@ sushildlh的評論,你可能錯誤地實現了'Parcelable'。 – Abbas

+0

我還沒有實現Parcelable。我只是在碎片之間傳遞簡單的數據類型。 – dutchman711

回答

0

我相信我的問題是,在片段A監聽內部類是沒有Parcelable或序列化。我將偵聽器定義爲阻止異常的瞬態變量。

private transient FragmentAFragment.ListSelectorListener listener = new FragmentAFragment.ListSelectorListener() { 
    @Override 
    public void onSelected(final String selected) { 
    } 
}; 

// then use the listener to start Fragment B 
BaseActivity activity = (BaseActivity) getActivity(); 
activity.addFragment(R.id.container, FragmentBFragment.newInstance(getString(R.string.input_list), selectedListItem, listener));