与自定义可序列化对象的数组列表的意图错误

时间:2017-05-12 14:17:07

标签: android android-intent arraylist serializable

我正在尝试将自定义对象的arraylist从一个活动传递给另一个。 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; }
}

其中也实现了serializable。

要将数据从一个活动传递到另一个活动,我在原始活动上使用它:

 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) 

0 个答案:

没有答案