如何解决 java.lang.RuntimeException: Parcelable 写序列化对象遇到IOException

How to solve java.lang.RuntimeException: Parcelable encountered IOException writing serializable object

在我的 Android 应用程序中,我想将我的自定义对象传递给另一个 activity.I 我正在使用意图

Intent i = new Intent(A.this,B.class); 
i.putExtra ("Object", myObject);
context.startActivity (i);

正在调用activity

 MyObject myObject = (MyObject) intent.getSerializableExtra ("Object");

当我 运行 它显示的应用程序

时,我还在我的模型 class.But 中实现了 Serilizable

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object

日志显示了这一点

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.eventmate2.model.Event)
    at android.os.Parcel.writeSerializable(Parcel.java:1394)
    at android.os.Parcel.writeValue(Parcel.java:1341)
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:644)
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
    at android.os.Bundle.writeToParcel(Bundle.java:1034)
    at android.os.Parcel.writeBundle(Parcel.java:669)
    at android.content.Intent.writeToParcel(Intent.java:7501)
    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2479)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496)
    at android.app.Activity.startActivityForResult(Activity.java:3760)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
    at android.app.Activity.startActivityForResult(Activity.java:3721)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
    at android.app.Activity.startActivity(Activity.java:4033)
    at android.app.Activity.startActivity(Activity.java:3999)
    at com.eventmate2.adapter.HomeAdapter.onMenuItemClick(HomeAdapter.java:188)
    at android.support.v7.widget.PopupMenu.onMenuItemSelected(PopupMenu.java:113)
    at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
    at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
    at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:128)
    at android.widget.AdapterView.performItemClick(AdapterView.java:305)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
    at android.widget.AbsListView.run(AbsListView.java:3865)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5345)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
 Caused by: java.io.NotSerializableException: com.eventmate2.model.Age
    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    at java.util.ArrayList.writeObject(ArrayList.java:648)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1033)
    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    at android.os.Parcel.writeSerializable(Parcel.java:1389)
    at android.os.Parcel.writeValue(Parcel.java:1341) 
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:644) 
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313) 
    at android.os.Bundle.writeToParcel(Bundle.java:1034) 
    at android.os.Parcel.writeBundle(Parcel.java:669) 
    at android.content.Intent.writeToParcel(Intent.java:7501) 
    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2479) 
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1496) 
    at android.app.Activity.startActivityForResult(Activity.java:3760) 
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767) 
    at android.app.Activity.startActivityForResult(Activity.java:3721) 
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754) 
    at android.app.Activity.startActivity(Activity.java:4033) 
    at android.app.Activity.startActivity(Activity.java:3999) 
    at com.eventmate2.adapter.HomeAdapter.onMenuItemClick(HomeAdapter.java:188) 
    at android.support.v7.widget.PopupMenu.onMenuItemSelected(PopupMenu.java:113) 
    at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840) 
    at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158) 
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991) 
    at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:128) 
    at android.widget.AdapterView.performItemClick(AdapterView.java:305) 
    at android.widget.AbsListView.performItemClick(AbsListView.java:1146) 
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053) 
    at android.widget.AbsListView.run(AbsListView.java:3865) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5345) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742) 

在堆栈跟踪中,您可以看到

Caused by: java.io.NotSerializableException: com.eventmate2.model.Age

Age class 没有实现 Serializable 接口。实施将解决此问题

Class Age 。必须实现 Serializable 接口。使其对象序列化。