我正在尝试在两个活动之间传递 Parcelable 对象的 ArrayList 以及字符串值。这是将数据添加到意图并将其传递的代码:
Intent intent = new Intent(this, DisplayLotListActivity.class);
Bundle dataBundle = new Bundle();
dataBundle.putParcelableArrayList(DisplayLotListActivity.EXTRA_LOT_ARRAY, lotList);
dataBundle.putString(EXTRA_LOT_NUMBER, lotNumber);
intent.putExtra(DisplayLotListActivity.EXTRA_DATA, dataBundle);
startActivity(intent);
这是我用来从目标活动的意图中获取数据的代码:
Intent intent = getIntent();
Bundle dataBundle = intent.getBundleExtra(EXTRA_DATA);
lotList = dataBundle.getParcelableArrayList(EXTRA_LOT_ARRAY);
lotNumber = dataBundle.getString(LotInquiryActivity.EXTRA_LOT_NUMBER);
当我检查调试器时,数据结构在调用活动之前看起来是正确的,但是当我进入目标活动时,数据结构已损坏。特别是 ArrayList 作为 3 个元素,它的大小仍然是 3 个元素,但第二个元素为空。捆绑包中还有一个额外的额外内容,其中包含带有空键的缺失元素对象。我之前和之后都有调试器的图像,但由于反垃圾邮件规则,无法将它们放在帖子中。
之前: http: //i.stack.imgur.com/vDipq.png 之后:http: //i.stack.imgur.com/JqbF7.png
有什么我想念的吗?无论我使用 Bundle 还是将 ArrayList 直接添加到意图,都会出现此问题。这是在运行 4.0.3 的三星 Tab 2 上运行的。4.0 模拟器也会出现这种情况。
[编辑] 这是 Parcelable 正在使用的对象(我刚刚离开了底部的 getter 和 setter 方法)
public class Lot implements Parcelable{
private String lotn;
private String dsc1;
private String dsc2;
private String litm;
private long itm;
private String locn;
private String mcu;
private String uom1;
private String uom2;
private BigDecimal pqav;
private BigDecimal pqoh;
private BigDecimal sqoh;
private long vend;
private String rlot;
private String ldsc;
private String lots;
private String lot1;
private String lot2;
private String lot3;
private String lotsdsc;
private XMLGregorianCalendar mmej;
private XMLGregorianCalendar ohdj;
public Lot(){
}
public Lot(Parcel source){
lotn = source.readString();
dsc1 = source.readString();
dsc2 = source.readString();
litm = source.readString();
locn = source.readString();
mcu = source.readString();
uom1 = source.readString();
uom2 = source.readString();
itm = source.readLong();
pqav = new BigDecimal(source.readString());
pqoh = new BigDecimal(source.readString());
sqoh = new BigDecimal(source.readString());
vend = source.readLong();
rlot = source.readString();
ldsc = source.readString();
lots = source.readString();
lot1 = source.readString();
lot2 = source.readString();
lot3 = source.readString();
lotsdsc = source.readString();
try{
mmej = DatatypeFactory.newInstance().newXMLGregorianCalendar(source.readString());
}catch (Exception exc){
mmej = null;
}
try{
ohdj = DatatypeFactory.newInstance().newXMLGregorianCalendar(source.readString());
}catch (Exception exc){
ohdj = null;
}
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(lotn);
dest.writeString(dsc1);
dest.writeString(dsc2);
dest.writeString(litm);
dest.writeString(locn);
dest.writeString(mcu);
dest.writeString(uom1);
dest.writeString(uom2);
dest.writeLong(itm);
if(pqav != null){
dest.writeString(pqav.toPlainString());
} else {
dest.writeString("0");
}
if(pqoh != null){
dest.writeString(pqoh.toPlainString());
} else {
dest.writeString("0");
}
if(sqoh != null){
dest.writeString(sqoh.toPlainString());
} else {
dest.writeString("0");
}
dest.writeLong(vend);
dest.writeString(rlot);
dest.writeString(ldsc);
dest.writeString(lots);
dest.writeString(lot1);
dest.writeString(lot2);
dest.writeString(lot3);
dest.writeString(lotsdsc);
if(mmej != null){
dest.writeString(mmej.toXMLFormat());
} else {
dest.writeString("");
}
if(ohdj != null){
dest.writeString(ohdj.toXMLFormat());
} else {
dest.writeString("");
}
}
/**
*
*/
public static final Parcelable.Creator<Lot> CREATOR
= new Parcelable.Creator<Lot>() {
public Lot createFromParcel(Parcel in) {
return new Lot(in);
}
public Lot[] newArray(int size) {
return new Lot[size];
}
};