Search code examples
javaandroidparcel

Class not found when unmarshalling: Parcelable (Android)


I am trying to pass array of objects from one activity to another activity using parcelable. Here i faced this problem Class not found when unmarshalling:

First Activity code

  intent.setExtrasClassLoader(MenueItemDetails.class.getClassLoader());
  intent.putExtra("menue",myArray);

Second Activity code

 myArray  = (MenueItemDetails[])getIntent().getParcelableArrayExtra("menue");

it's my class which is parceable

public class MenueItemDetails implements Parcelable {

private int id = 0, menueId = 0, type = 0, typeId = 0, styleId = 0, lineBefore = 0;
private String webSite = "", title = "", icon = "";


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

// write your object's data to the passed-in Parcel
@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(id);
    out.writeInt(menueId);
    out.writeInt(type);
    out.writeInt(typeId);
    out.writeInt(styleId);
    out.writeInt(lineBefore);
    out.writeString(webSite);
    out.writeString(title);
    out.writeString(icon);
}
private MenueItemDetails(Parcel in) {
    id = in.readInt();
    menueId = in.readInt();
    type = in.readInt();
    typeId = in.readInt();
    styleId= in.readInt();
    lineBefore= in.readInt();
    webSite=in.readString();
    title= in.readString();
    icon=in.readString();
}
public MenueItemDetails() {
    id = 0;
    menueId = 0;
    type = 0;
    styleId= 0;
    lineBefore= 0;
    webSite="";
    title= "";
    icon="";
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public MenueItemDetails createFromParcel(Parcel in) {
        return new MenueItemDetails(in);
    }

    public MenueItemDetails[] newArray(int size) {
        return new MenueItemDetails[size];
    }
};

public int getLineBefore() {
    return lineBefore;
}

public void setLineBefore(int lineBefore) {
    this.lineBefore = lineBefore;
}

public void setId(int id) {
    this.id = id;
}

public void setMenueId(int menueId) {
    this.menueId = menueId;
}

public void setTitle(String title) {
    this.title = title;
}

public void setIcon(String icon) {
    this.icon = icon;
}

public void setType(int type) {
    this.type = type;
}

public void setTypeId(int typeId) {
    this.typeId = typeId;
}

public void setStyleId(int styleId) {
    this.styleId = styleId;
}

public int getId() {
    return id;
}

public String getWebSite() {
    return webSite;
}

public void setWebSite(String webSite) {
    this.webSite = webSite;
}

public int getMenueId() {
    return menueId;
}

public String getTitle() {
    return title;
}

public String getIcon() {
    return icon;
}

public int getType() {
    return type;
}

public int getTypeId() {
    return typeId;
}

public int getStyleId() {
    return styleId;
}

}


Solution

  • Your Second Activity must be like this:

    Intent intent = getIntent();
    intent.setExtrasClassLoader(MenueItemDetails.class.getClassLoader());
    myArray = (MenueItemDetails[]) intent.getParcelableArrayExtra("menue");