gpt4 book ai didi

android - 如何通过实现 parcable 通过 Intent 发送类对象。无法编码值错误

转载 作者:行者123 更新时间:2023-11-29 17:14:34 25 4
gpt4 key购买 nike

我有一个类,其中有数组列表。如何通过 Intent 发送它。以下是我为我的类(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];
}
};
}

请看一下更新的我收到类似unable to marshal value 的错误我让所有类(class)都可以打包。如何通过Intent发送它的对象提前致谢

Logcat 就像

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 获取异常

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

最佳答案

您还需要在 Item 和 Coupon 对象中实现 Parcelable

然后当您在 MakeOrder 类中添加和读取列表时:

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

顺便说一句,你的构造函数是空的

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

此链接可以帮助您:

How to make a class with nested objects Parcelable

这是 Parcelable 文档:

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

编辑:

将更多 Percel 问题添加到问题中的位置。

聊天中的最终解决方案:https://chat.stackoverflow.com/rooms/123710/discussion-between-aman-singh-and-adalpari

关于android - 如何通过实现 parcable 通过 Intent 发送类对象。无法编码值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39571616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com