Class not found when unmarshalling: Parcelable (Android)

9.9k views Asked by At

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;
}

}

3

There are 3 answers

0
Ihor Dobrovolskyi On BEST ANSWER

Your Second Activity must be like this:

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

If your task is Pass data from activity to activity or Fragment

Than instead of Using Parcelable you can use Serializable object and pass it to intent it take very less time to implement and code than Parcelable

https://stackoverflow.com/a/39631604/4741746

implement serializable in your class

        public class Place implements Serializable{
        private int id;
        private String name;

        public void setId(int id) {
           this.id = id;
        }
        public int getId() {
           return id;
        }
        public String getName() {
           return name;
        }

        public void setName(String name) {
           this.name = name;
        }
        }

Then you can pass this object in intent

     Intent intent = new Intent(this, SecondAct.class);
     intent.putExtra("PLACE", Place);
     startActivity();

int the second activity you can get data like this

     Place place= (Place) getIntent().getSerializableExtra("PLACE");
0
Android Geek On

Your code to pass the arraylist in first activity code is not correct.Send the arraylist in your activities as below:

First Activity Code

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

And receive the arraylist as below in Second activity.

Second Activity Code

ArrayList<MenueItemDetails> myarray=getIntent().getParcelableArrayListExtra("menue");

The problem with your code is that it is used to send and receive single Object,not the arraylist.If you still have problems in using Parceable object,make sure to use Android Parceable Code generator plugin for Android Studio.