Android Bundle在活动之间传递时损坏了吗?

时间:2012-10-30 11:39:38

标签: android android-intent android-activity

我尝试传递Parcelable对象的ArrayList以及两个活动之间的字符串值。这是将数据添加到intent并通过它的代码:

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个元素,但第二个元素为null。然后在包中有一个额外的额外包含缺少的元素对象和一个空键。我之前和之后都有调试器的图像,但由于反垃圾邮件规则,无法将它们放在帖子中。

之前:http://i.stack.imgur.com/vDipq.png 之后:http://i.stack.imgur.com/JqbF7.png

我有什么遗失的东西吗?无论我使用Bundle还是直接将ArrayList添加到intent中,都会出现此问题。这是在运行4.0.3的Samsung 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];
     }
 };

1 个答案:

答案 0 :(得分:3)

好的,对于那些回到这个问题的人来说,我发现有两个问题,我认为它们会导致这种行为。这些问题都与我使用的Parcelable对象有关,所以感谢Todd至少指出了我这个方向。

首先,我有一个简单的错误,我在Parcelable对象的构造函数中错过了一个readString()。所以基本上我写了n个元素并读取n - 1个元素。第二个问题是Android没有实现javax.xml.datatype库,这意味着XMLGregorianCalendar不可用。因为我不需要客户端这个类的功能(它与之交谈的Java Web应用程序使用它)我只是转换为一个简单的java.util.Date对象。