自定义可序列化对象数组列表的意图错误
Posted
技术标签:
【中文标题】自定义可序列化对象数组列表的意图错误【英文标题】:Intent error with array list of custom serializable objects 【发布时间】:2017-10-11 21:45:36 【问题描述】:我正在尝试将自定义对象的数组列表从一个活动传递到另一个活动。 arrayList 包含非常简单的对象。这是课程:
public class MaPin implements Serializable
private Location pLocation;
private String pMessage;
public MaPin(Location location, String message)
this.pLocation = location;
this.pMessage = message;
public Location getpLocation() return this.pLocation;
public String getpMessage() return this.pMessage;
如您所见,它实现了可序列化。然后根据这个帖子:Pass arraylist of user defined objects to Intent android,使用Data Wrapper很方便。所以我用了一个:
public class DataWrapper implements Serializable
private ArrayList<MaPin> mMapArr;
public DataWrapper(ArrayList<MaPin> data) this.mMapArr = data;
public ArrayList<MaPin> getmMapArr() return this.mMapArr;
这也实现了可序列化。
要将数据从一个活动传递到另一个活动,我在源活动上使用它:
Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
intent.putExtra(EXTRA_MESSAGE, new DataWrapper(mMapinHistory));
startActivity(intent);
其中mMapinHistory
是`MaPin 的ArrayList
。
并接收我在目标活动中使用它:
Intent intent = getIntent();
DataWrapper wp = (DataWrapper) getIntent().getSerializableExtra(MainActivity.EXTRA_MESSAGE);
mPositionHistory = wp.getmMapArr();
如您所见,我认为我尊重帖子中提到的内容。但是,我仍然有以下错误:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = ovh.trimard.locapp.DataWrapper)
我不知道为什么,因为 DataWrapper 和 MaPin 都实现了可序列化!
我做错了什么?
编辑
按要求完成 Stacktrace:
05-12 15:11:32.370 2531-2531/ovh.trimard.locapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: ovh.trimard.locapp, PID: 2531
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = ovh.trimard.locapp.DataWrapper)
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:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.content.Intent.writeToParcel(Intent.java:8687)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4490)
at ovh.trimard.locapp.MainActivity$3.onClick(MainActivity.java:152)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.io.NotSerializableException: android.location.Location
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 java.util.ArrayList.writeObject(ArrayList.java:734)
at java.lang.reflect.Method.invoke(Native Method)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:977)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1536)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
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:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.content.Intent.writeToParcel(Intent.java:8687)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3082)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4490)
at ovh.trimard.locapp.MainActivity$3.onClick(MainActivity.java:152)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
【问题讨论】:
请编辑您的问题并发布整个 Java 堆栈跟踪,而不仅仅是错误消息。 Location 类实现 Serialisable? 我不知道Location,这是我没有修改的默认类。 (实际上我对 Java 和 Android 还是很陌生)。你认为 Location 应该是可序列化的吗? 【参考方案1】:Location 类没有实现 Serializable
Caused by: java.io.NotSerializableException: android.location.Location
为了正确打包数据,您的所有内部类都应该实现可打包/可序列化
【讨论】:
感谢您的回答,显然位置不可序列化,我想我必须找到解决方法.. 如果位置是谷歌地图位置,您可以简单地提取纬度/经度并分配给您的自定义字段 谢谢,这就是我要做的!再次感谢您的支持! 很高兴为您提供帮助:)以上是关于自定义可序列化对象数组列表的意图错误的主要内容,如果未能解决你的问题,请参考以下文章
Protobuf-net:嵌套的 IEnumerable 对象