Android 如何通过实现parcable通过intent发送类对象。无法封送值错误
我有一个类,其中有数组列表。如何通过意图发送它。接下来我为我的班级做了什么Android 如何通过实现parcable通过intent发送类对象。无法封送值错误,android,object,android-intent,parcelable,Android,Object,Android Intent,Parcelable,我有一个类,其中有数组列表。如何通过意图发送它。接下来我为我的班级做了什么 public class MakeOrder implements Parcelable { @SerializedName("refno") @Expose private String refno; @SerializedName("ddesc") @Expose private String ddesc; @SerializedName("free") @Expose private String free; @
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];
}
};
}
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];
}
};
}
公共类项实现可包裹{
@SerializedName(“itemid”)
@暴露
私有字符串itemid;
@序列化名称(“数量”)
@暴露
私有字符串数量;
@序列化名称(“子”)
@暴露
private List sub=new ArrayList();
/**
*
*@返回
*项目ID
*/
公共字符串getItemid(){
返回itemid;
}
/**
*
*@param itemid
*项目ID
*/
公共void setItemid(字符串itemid){
this.itemid=itemid;
}
/**
*
*@返回
*数量
*/
公共字符串getQty(){
退货数量;
}
/**
*
*@param数量
*数量
*/
公共作废设置数量(字符串数量){
该数量=数量;
}
/**
*
*@返回
*潜艇
*/
公共列表getSub(){
返回子节点;
}
/**
*
*@param sub
*潜艇
*/
公共无效设置子项(列表子项){
this.sub=sub;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
dest.writeString(this.itemid);
目的写入(该数量);
目的地书面清单(本文件);
}
公共项目(){
}
受保护物品(包裹中){
this.itemid=in.readString();
this.qty=in.readString();
this.sub=新的ArrayList();
in.readList(this.sub,sub.class.getClassLoader());
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共项目createFromParcel(地块源){
返回新项目(来源);
}
@凌驾
公共项[]新数组(整数大小){
返回新项目[大小];
}
};
}
尝试使用此
intent.putParcelableArrayListExtra("value", orderList);
在其他活动中,可将其作为:
List<String> orderList=this.getIntent().
getParcelableArrayListExtra("value");
List orderList=this.getIntent()。
getParcelableArrayListExtra(“价值”);
希望这有助于您需要在物品和优惠券对象中实现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);
}
private MakeOrder(包裹中){
...
//阅读项目列表
items=newarraylist();
in.readList(items,Item.CREATOR);
//读取优惠券对象的步骤
in.readParcelable(优惠券.CREATOR);
}
此链接可以帮助您:
这是可包裹的文件:
编辑:
where more Percel问题增加了这个问题
聊天中的最终解决方案:首先,您的
包裹
构造函数为空。如果你让它空着,它就不会工作了。您需要按照在writeToParcel()
method中写入字段的顺序读取包裹中的字段
然后,要将可打包的数据写入intent,请使用intent的方法。要从intent提取数据,请使用intent的
getParcelableExtra()
方法您需要添加如下额外内容:
Intent intent-new Intent(this,YouActvity.class);
intent.putExtra("key",yourModel);
startActivity(intent);
在要检索的活动中:
YouModel yourModel = (YourModel) getIntent().getSerializableExtra("key");
你的答案不是关于Parcelable,而是关于Serializable谢谢你告诉我这个错误,我已经改变了我的答案,这样你就可以直接将列表添加到意图中,即对象内部。如果你想传递整个对象,这不是很好的结构。它是空的,因为我无法找到包裹列表值的方法。好的,试试这个解决方案,告诉我它是否对你有帮助。什么是其他对象,如优惠券如何发送它,我需要使优惠券类也包裹。(Y) 现在让我检查所有。我完成了所有操作,但遇到异常,如无法封送值。现在有什么问题?实际上我的问题是如何传递列表项?你能发布完整的日志吗?错误行?请现在检查,我更新了Item.java第90行的问题是什么?这是他的错误。无论如何,这不是一个问题被问到的方式。首先你问了一些问题,在我们解决它之后,你改变了所有的问题,而不是添加新的问题(标题、代码)。现在给出的答案毫无意义..哎呀!!!你是ryt。那是我的错误
YouModel yourModel = (YourModel) getIntent().getSerializableExtra("key");