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

时间:2019-02-20 05:34:13

标签: java android runtimeexception

在我的Android应用中,我想将自定义对象传递给另一个活动。我正在使用意图

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

通话中

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

我也在模型类中实现了Serilizable。但是当我运行应用程序时它会显示

  

java.lang.RuntimeException:可打包遇到IOException写入可序列化对象

日志显示

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$2$1.onMenuItemClick(HomeAdapter.java:188)
    at android.support.v7.widget.PopupMenu$1.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$3.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$2$1.onMenuItemClick(HomeAdapter.java:188) 
    at android.support.v7.widget.PopupMenu$1.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$3.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) 

2 个答案:

答案 0 :(得分:1)

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

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

Age类未实现Serializable接口。实施将解决此问题

答案 1 :(得分:0)

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