0

I am trying to pass a arraylist of custom objects from an activity to another. The arrayList contains very simple objects. Here is the class :

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; } 
}

As you can see, it implements serializable. Then according to this post : Pass arraylist of user defined objects to Intent android , It is convenientto use a Data Wrapper. So I used one :

public class DataWrapper implements Serializable {

    private ArrayList<MaPin> mMapArr;

    public DataWrapper(ArrayList<MaPin> data) { this.mMapArr = data; }

    public ArrayList<MaPin> getmMapArr() { return this.mMapArr; }
}

Which also implements serializable.

To pass data from one activity to another I use this on the origin activity :

 Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
 intent.putExtra(EXTRA_MESSAGE, new DataWrapper(mMapinHistory));
 startActivity(intent);

Where mMapinHistory is the ArrayList of `MaPin. And to receive I use this on the destination activity :

 Intent intent = getIntent();
 DataWrapper wp = (DataWrapper) getIntent().getSerializableExtra(MainActivity.EXTRA_MESSAGE);
 mPositionHistory = wp.getmMapArr();

So as you can see I think I respected what's mentioned in the post. However, I still have the following error :

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = ovh.trimard.locapp.DataWrapper)

And I don't know why since both DataWrapper and MaPin implements serializable !

What am I doing wrong ?

EDIT

Complete Stacktrace as requested :

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) 
3
  • 1
    Please edit your question and post the entire Java stack trace, not just the error message. Commented May 12, 2017 at 14:17
  • Location class implements Serialisable? Commented May 12, 2017 at 15:04
  • I don't know about Location, it's the defautl class I didn't modify it. (I'm quite new to Java and Android actually). Do you think Location should be serializable ? Commented May 12, 2017 at 15:14

1 Answer 1

2

Location class is not implementing Serializable

Caused by: java.io.NotSerializableException: android.location.Location

In order to parcel data properly all your inner classes should implement parcelable/serialisable

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, apparently Location is not serializable, I have to find a way around it I guess..
If location is Google Maps Location than you can just simply extract lat/long and assign to your custom fields
Thanks, that's what I'm going to do! Thanks again for the support !
Glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.