在活动之间传递ArrayList

时间:2012-12-12 00:35:27

标签: android android-intent parcelable

我有一个活动,我将对象添加到实现Parcelable接口的ArrayList中。然后我通过bundle将此列表传递给另一个活动但是当我尝试在新活动中打印列表的大小时出现以下错误:

  

java.lang.RuntimeException:无法恢复活动{com.example.test / com.example.test.SectionsActivity}:java.lang.NullPointerException

这是我添加到列表的第一个活动:

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Toast.makeText(getApplicationContext(), l.getItemAtPosition(position).toString() + " added to order", Toast.LENGTH_SHORT).show();
    // add clicked item to orderData....
    MenuItem m = (MenuItem) l.getItemAtPosition(position);
    // create new item
    orderData.add(m);
    subTotal += m.getPrice();
    calc();
}

这是我通过意图发送数据的第一个活动:

confirmBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            b.putParcelable("order", orderData);
            Intent i = new Intent(v.getContext(), SectionsActivity.class);
            i.putExtra("data",b);
            startActivity(i);

        }
    });

这是我尝试检索包并显示大小的第二个活动:

@Override
protected void onResume() {
    super.onResume();
    getIntentData();
}


public void getIntentData(){
    Intent i = getIntent();
    if(i != null & i.hasExtra("data")){
        Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_LONG).show();
        b = i.getExtras();
        orderData = b.getParcelable("order");
        int size = orderData.size();
        Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_LONG).show();

    }
}

为什么我得到空指针的任何想法?这让我很生气!

orderData是一个MenuItemList对象:

public class MenuItemList extends ArrayList <MenuItem> implements Parcelable{

/**
 * 
 */
private static final long serialVersionUID = 2998684930374219271L;



public MenuItemList(){

}

public static final Parcelable.Creator<MenuItemList> CREATOR = new Parcelable.Creator<MenuItemList>() {

    @Override
    public MenuItemList createFromParcel(Parcel source) {
        return new MenuItemList(source);
    }

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


};


public MenuItemList(Parcel source) {
        readFromParcel(source);
}

private void readFromParcel(Parcel source) {

    this.clear();

    //read the size of the list
    int size = source.readInt();

    //Remember order of items written into the Parcel. Important here.
    for(int i = 0; i < size; i ++){
        MenuItem item = new MenuItem();
        item.setName(source.readString());
        item.setPrice(source.readDouble());
        this.add(item);
    }
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    int size = this.size();

    dest.writeInt(size);

    for(int i = 0; i < size; i ++){
        MenuItem item = this.get(i);
        dest.writeString(item.getName());
        dest.writeDouble(item.getPrice());

    }

}

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

}

解决问题的代码更改:

已更改onClick(查看v):

@Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), SectionsActivity.class);
            i.putExtra("data", (ArrayList<MenuItem>)orderData);
            startActivity(i);

        }

CHANGED getIntent():

public void getIntentData(){
    Intent i = getIntent();
    if(i != null && i.hasExtra("data")){
        Toast.makeText(this.getApplicationContext(), "recieved", Toast.LENGTH_LONG).show();
        orderData = i.getParcelableExtra("data");
        int size = orderData.size();
        Toast.makeText(this.getApplicationContext(), String.valueOf(size), Toast.LENGTH_LONG).show();

    }
}

2 个答案:

答案 0 :(得分:5)

<强>重写
问题是你有一个额外的捆绑。目前在getIntentData(),您必须致电:

getIntent()                     // Fetch the Intent, 
    .getExtras()                // Fetch the Bundle of extras, 
    .getBundle("data")          // Fetch the Bundle "data", 
    .getParcelable("order");    // Then get your parcelable...  

让我们删除不必要的Bundle。

public void onClick(View v) {
    Intent i = new Intent(v.getContext(), SectionsActivity.class);
    i.putExtra("data", orderData);
    startActivity(i);
}

现在更新getIntentData()

Intent i = getIntent();
if(i != null && i.hasExtra("data")){
    orderData = i.getParcelableExtra("data");
    ...
}

答案 1 :(得分:4)

除非我遗漏了某些内容,否则您在&函数中使用按位&&代替逻辑getIntentData()