java Parcel@e7a33b1:正在解组未知类型的数组列表

java Parcel@e7a33b1: Unmarshalling unknown type of arraylist

我正在尝试使用 Parcelable 在 android 中传递数据,但是当我以正确的类型使用它时,我在这一行中遇到了一个错误,我不知道我错过了什么这里。

这是打包时出现问题的对象:

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

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

这里是读取数据的parcel构造函数:

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;

}

我是这样写的:

@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));

}

我收到一个错误:

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

在这行代码中:

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

您应该使用其他方法来写入和读取 Parcelable 的列表:

  1. 使用writeTypedList写入Parcel

    parcel.writeTypedList(secondChildCategories);

  2. 使用createTypedArrayList for reading from Parcel (alternatively readTypedList可以使用)

    secondChildCategories = in.createTypedArrayList(SecondChildCategory.CREATOR);

希望对您有所帮助。