我有可以打开3个不同片段的父片段,3个片段中的每一个都可以更新父片段中的相同字段。 所以我创建了超级简单的监听器:
public interface RatingUpdatedListener extends Serializable{
//allow to set new rating from Gallery, Details, PersonalModel fragments into MyProfileFragment
void onRatingUpdated(int rating);
}
当我加载任何子片段时,我通过FragmentA.setArguments(bundle)
添加Bundle bundle = new Bundle();
bundle.putSerializable(RATING_LISTENER, mRatingListener);
fragment.setArguments(bundle);
和内部父片段我实现了这个监听器
private RatingUpdatedListener mRatingListener = new RatingUpdatedListener() {
@Override
public void onRatingUpdated(int rating) {
}
};
然后在子片段中我抓住了这个监听器:
mRatingUpdatedListener = (RatingUpdatedListener) getArguments().getSerializable(MyProfileFragment.RATING_LISTENER);
当我在其中一个子片段中使用本机android方法打开CameraActivity或GalleryActivity时出现问题,我得到了:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object
这里是完整的例外:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.iseeya.iseeya.fragments.MyProfileFragment)
at android.os.Parcel.writeSerializable(Parcel.java:1527)
at android.os.Parcel.writeValue(Parcel.java:1475)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1423)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:157)
at android.os.Parcel.writeTypedArray(Parcel.java:1307)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617)
at android.os.Parcel.writeParcelable(Parcel.java:1496)
at android.os.Parcel.writeValue(Parcel.java:1402)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1423)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3683)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3830)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6276)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:788)
Caused by: java.io.NotSerializableException: android.support.v7.widget.AppCompatImageView
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:1522)
at android.os.Parcel.writeValue(Parcel.java:1475)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1423)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:157)
at android.os.Parcel.writeTypedArray(Parcel.java:1307)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617)
at android.os.Parcel.writeParcelable(Parcel.java:1496)
at android.os.Parcel.writeValue(Parcel.java:1402)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1423)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3683)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3830)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6276)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:788)
如果我的听众没有任何对象,问题为什么在NotSerializableException
失败?