Monday, August 9, 2010

ClassNotFound exception when unmarshalling

The following code is the serialization part of the Category class in PackingList project. Since the category has an arraylist of items, I have to put it in Bundle. To de-serialize it, I need to retrieve items from the Bundle. To do that, I was using Bundle d = in.readBundle(). However, I received “ClassNotFound exception when unmarshalling”. I have to pass in the ClassLoader so it knows it’s for the Item.

 

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(category);
Bundle b = new Bundle();
b.putParcelableArrayList("items", items);
dest.writeBundle(b);

}

public static final Parcelable.Creator<Category> CREATOR =
new Parcelable.Creator<Category>() {
public Category createFromParcel(Parcel in) {
Category category = new Category();
category.category = in.readString();
Bundle b = in.readBundle(Item.class.getClassLoader());
category.items = b.getParcelableArrayList("items");

return category;
}

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

3 comments:

  1. thanks for sharing this i have one question how can we do the marsahlling and unmarshalling, if

    for example: in your "items" also have a array list

    ReplyDelete
  2. if i add the same idea for the array list in items i'm getting now i'm getting “readBundle: bad magic number” please give another tip

    ReplyDelete
  3. I am not sure about the issue since I don't know what your code looks like, but you may want to make sure the order of the properties is the same in writeToParcel() and createFromParcel().

    ReplyDelete