Search code examples
javaandroidarraylistparcelableparcel

java Parcel@e7a33b1: Unmarshalling unknown type of arraylist


I'm trying to use Parcelable to pass data in android and but I got an error in this line while I use it in the correct type I didn't know what I miss here.

Here is the object which had the problem in parceling:

ArrayList<SecondChildCategory> secondChildCategories;
   public ArrayList<SecondChildCategory> getSecondChildCategories() {
        return secondChildCategories;
    }

    public void setSecondChildCategories(ArrayList<SecondChildCategory> secondChildCategories) {
        this.secondChildCategories = secondChildCategories;
    }

Here the parcel constructor for reading data:

protected ChildCategory(Parcel in) {
    if (in.readByte() == 0) {
        id = null;
    } else {
        id = in.readInt();
    }
    image = in.readString();
    softDelete = in.readString();
    if (in.readByte() == 0) {
        productCategoryId = 0;
    } else {
        productCategoryId = in.readInt();
    }
    createdAt = in.readString();
    updatedAt = in.readString();
    parentCategoryID = in.readString();
    backgroundColor = in.readString();
    name = in.readString();
    secondChildCategories = in.readArrayList(SecondChildCategory.class.getClassLoader()); // error reported here 
    hasChild = in.readByte() != 0;

}

and here how I write it:

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(id);
    parcel.writeString(image);
    parcel.writeString(softDelete);
    parcel.writeInt(productCategoryId);
    parcel.writeString(createdAt);
    parcel.writeString(updatedAt);
    parcel.writeString(parentCategoryID);
    parcel.writeString(backgroundColor);
    parcel.writeString(name);
    parcel.writeList(secondChildCategories);
    parcel.writeByte((byte) (hasChild ? 1 : 0));

}

I got an error:

Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@e7a33b1: Unmarshalling unknown type code 7143535 at offset 372

on this line of code:

secondChildCategories = in.readArrayList(SecondChildCategory.class.getClassLoader());

Solution

  • You should use other methods to write and read list of Parcelables:

    1. use writeTypedList for writing into Parcel

      parcel.writeTypedList(secondChildCategories);

    2. use createTypedArrayList for reading from Parcel (alternatively readTypedList can be used)

      secondChildCategories = in.createTypedArrayList(SecondChildCategory.CREATOR);

    Hope that helps.