Search code examples
androidobjectandroid-intentparcelable

How to send class object through intent by implement parcable. Unable to marshal value error


I've one class, in which array list is there. How to send it through intent. following i did for my class

public class MakeOrder implements Parcelable {

@SerializedName("refno")
@Expose
private String refno;
@SerializedName("ddesc")
@Expose
private String ddesc;
@SerializedName("free")
@Expose
private String free;
@SerializedName("fgift")
@Expose
private String fgift;
@SerializedName("sgift")
@Expose
private String sgift;
@SerializedName("sandage")
@Expose
private SandageResponse sandage;
@SerializedName("inst")
@Expose
private String inst;
@SerializedName("items")
@Expose
private List<Item> items = new ArrayList<Item>();
@SerializedName("discount")
@Expose
private String discount;
@SerializedName("coupon")
@Expose
private Coupon coupon;
@SerializedName("delivery")
@Expose
private String delivery;
@SerializedName("user")
@Expose
private User user;
@SerializedName("otype")
@Expose
private String otype;
@SerializedName("ptype")
@Expose
private String ptype;
@SerializedName("app_id")
@Expose
private String appId;
@SerializedName("app_key")
@Expose
private String appKey;
@SerializedName("request")
@Expose
private String request;
@SerializedName("tablename")
@Expose
private String tablename;
@SerializedName("staff")
@Expose
private String staff;

public String getTablename() {
    return tablename;
}

public void setTablename(String tablename) {
    this.tablename = tablename;
}

public String getStaff() {
    return staff;
}

public void setStaff(String staff) {
    this.staff = staff;
}

public String getRefno() {
    return refno;
}

public void setRefno(String refno) {
    this.refno = refno;
}

public String getDdesc() {
    return ddesc;
}

public void setDdesc(String ddesc) {
    this.ddesc = ddesc;
}

public String getFree() {
    return free;
}

public void setFree(String free) {
    this.free = free;
}

public String getFgift() {
    return fgift;
}

public void setFgift(String fgift) {
    this.fgift = fgift;
}

public String getSgift() {
    return sgift;
}

public void setSgift(String sgift) {
    this.sgift = sgift;
}

public SandageResponse getSandage() {
    return sandage;
}

public void setSandage(SandageResponse sandage) {
    this.sandage = sandage;
}

public String getInst() {
    return inst;
}

public void setInst(String inst) {
    this.inst = inst;
}

public List<Item> getItems() {
    return items;
}

public void setItems(List<Item> items) {
    this.items = items;
}

public String getDiscount() {
    return discount;
}

public void setDiscount(String discount) {
    this.discount = discount;
}

public Coupon getCoupon() {
    return coupon;
}

public void setCoupon(Coupon coupon) {
    this.coupon = coupon;
}

public String getDelivery() {
    return delivery;
}

public void setDelivery(String delivery) {
    this.delivery = delivery;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public String getOtype() {
    return otype;
}

public void setOtype(String otype) {
    this.otype = otype;
}

public String getPtype() {
    return ptype;
}

public void setPtype(String ptype) {
    this.ptype = ptype;
}

public String getAppId() {
    return appId;
}

public void setAppId(String appId) {
    this.appId = appId;
}

public String getAppKey() {
    return appKey;
}

public void setAppKey(String appKey) {
    this.appKey = appKey;
}

public String getRequest() {
    return request;
}

public void setRequest(String request) {
    this.request = request;
}




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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.refno);
    dest.writeString(this.ddesc);
    dest.writeString(this.free);
    dest.writeString(this.fgift);
    dest.writeString(this.sgift);
    dest.writeParcelable(this.sandage, flags);
    dest.writeString(this.inst);
    dest.writeTypedList(this.items);
    dest.writeString(this.discount);
    dest.writeParcelable(this.coupon, flags);
    dest.writeString(this.delivery);
    dest.writeParcelable(this.user, flags);
    dest.writeString(this.otype);
    dest.writeString(this.ptype);
    dest.writeString(this.appId);
    dest.writeString(this.appKey);
    dest.writeString(this.request);
    dest.writeString(this.tablename);
    dest.writeString(this.staff);
}

public MakeOrder() {
}

protected MakeOrder(Parcel in) {
    this.refno = in.readString();
    this.ddesc = in.readString();
    this.free = in.readString();
    this.fgift = in.readString();
    this.sgift = in.readString();
    this.sandage = in.readParcelable(SandageResponse.class.getClassLoader());
    this.inst = in.readString();
    this.items = in.createTypedArrayList(Item.CREATOR);
    this.discount = in.readString();
    this.coupon = in.readParcelable(Coupon.class.getClassLoader());
    this.delivery = in.readString();
    this.user = in.readParcelable(User.class.getClassLoader());
    this.otype = in.readString();
    this.ptype = in.readString();
    this.appId = in.readString();
    this.appKey = in.readString();
    this.request = in.readString();
    this.tablename = in.readString();
    this.staff = in.readString();
}

public static final Parcelable.Creator<MakeOrder> CREATOR = new Parcelable.Creator<MakeOrder>() {
    @Override
    public MakeOrder createFromParcel(Parcel source) {
        return new MakeOrder(source);
    }

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

Please take a look of updated one I'm getting error like unable to marshal value I made all class Parcelable. how to send its object through Intent Thanks in Advance

Logcat is like

java.lang.RuntimeException: Parcel: unable to marshal value    Models.PlaceOrder.Sub@41e10f80
at android.os.Parcel.writeValue(Parcel.java:1235)
at android.os.Parcel.writeList(Parcel.java:622)
at Models.PlaceOrder.Item.writeToParcel(Item.java:90)
at android.os.Parcel.writeTypedList(Parcel.java:1017)
at Models.PlaceOrder.MakeOrder.writeToParcel(MakeOrder.java:246)
at android.os.Parcel.writeParcelable(Parcel.java:1254)
at android.os.Parcel.writeValue(Parcel.java:1173)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6866)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1897)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1418)
at android.app.Activity.internalStartActivityForResult(Activity.java:3427)
at android.app.Activity.access$200(Activity.java:660)
at android.app.Activity$2.onStartActivity(Activity.java:3417)
at android.app.Activity.startActivityForBusiness(Activity.java:5441)
at android.app.Activity.startActivityForResult(Activity.java:3413)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3367)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at android.app.Activity.startActivity(Activity.java:3644)
at android.app.Activity.startActivity(Activity.java:3612)

Items.java where getting Exception

public class Item implements Parcelable {

@SerializedName("itemid")
@Expose
private String itemid;
@SerializedName("qty")
@Expose
private String qty;
@SerializedName("sub")
@Expose
private List<Sub> sub = new ArrayList<Sub>();

/**
 *
 * @return
 * The itemid
 */
public String getItemid() {
    return itemid;
}

/**
 *
 * @param itemid
 * The itemid
 */
public void setItemid(String itemid) {
    this.itemid = itemid;
}

/**
 *
 * @return
 * The qty
 */
public String getQty() {
    return qty;
}

/**
 *
 * @param qty
 * The qty
 */
public void setQty(String qty) {
    this.qty = qty;
}

/**
 *
 * @return
 * The sub
 */
public List<Sub> getSub() {
    return sub;
}

/**
 *
 * @param sub
 * The sub
 */
public void setSub(List<Sub> sub) {
    this.sub = sub;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.itemid);
    dest.writeString(this.qty);
    dest.writeList(this.sub);
}

public Item() {
}

protected Item(Parcel in) {
    this.itemid = in.readString();
    this.qty = in.readString();
    this.sub = new ArrayList<Sub>();
    in.readList(this.sub, Sub.class.getClassLoader());
}

public static final Parcelable.Creator<Item> CREATOR = new Parcelable.Creator<Item>() {
    @Override
    public Item createFromParcel(Parcel source) {
        return new Item(source);
    }

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

Solution

  • You need to implement Parcelable also in the Item and Coupon objects.

    Then when you add and read the list in you MakeOrder class:

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ...
        // to write the Item list
        dest.writeList(items);
        // to write Coupon object
        dest.writeParcelable(coupon, falgs);
    }
    

    BTW, your constructor is empty

    private MakeOrder(Parcel in) {
        ...
        // to read the Item list
        items = new ArrayList<Item>();
        in.readList(items, Item.CREATOR);
        // to read Coupon object
        in.readParcelable(Coupon.CREATOR);
    }
    

    This link could help you:

    How to make a class with nested objects Parcelable

    And this is the Parcelable documentation:

    https://developer.android.com/reference/android/os/Parcel.html

    EDIT:

    The where more Percel problem added to the question.

    Final solution in chat: https://chat.stackoverflow.com/rooms/123710/discussion-between-aman-singh-and-adalpari