0

I must send an Object value to other Activity, so I found the way to using Parcelable Object. but this is the problem. The problem is not parcelable object can not be add Parcelable object.

    import android.os.Parcel;
    import android.os.Parcelable;
    
    import org.apache.http.Header;
    
    import java.util.ArrayList;

public class CustomParcelable implements Parcelable {

    // Extra Header
    private ArrayList<Header> headers = new ArrayList<Header>();

    public CustomParcelable() {}

    public CustomParcelable(ArrayList<Header> headers) {
        this.headers = headers;
    }

    // getter and setter
    //  ...

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(headers);
    }

    public static final Creator<CustomParcelable> CREATOR = new Creator<CustomParcelable>() {

        @Override
        public CustomParcelable createFromParcel(Parcel source) {
            ArrayList<Header> headers = new ArrayList<Header>();
            source.readList(headers, null);

            return new CustomParcelable( headers);
        }

        @Override
        public CustomParcelable[] newArray(int size) {
            return new CustomParcelable[size];
        }
    };
}

this code has exception like this.

IllegalArgumentException("Parcel: unknown type for value " + v);

Is anything missing on this? thank you.

lupin218
  • 23
  • 1
  • 1
  • 4

1 Answers1

1

Its should be

    @Override
    public CustomParcelable createFromParcel(Parcel source) {
        ArrayList<Header> headers = new ArrayList<Header>();
        source.readList("key", headers );

        return new CustomParcelable( source);
    }
sasikumar
  • 12,540
  • 3
  • 28
  • 48