1

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

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

}

解决问题的代码更改:

更改了点击(查看 v):

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

        }

改变 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();

    }
}
4

2 回答 2

5

重写
问题是你有一个额外的Bundle。目前,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");
    ...
}
于 2012-12-12T00:56:42.160 回答
4

除非我错过了什么,否则您在函数中使用的是按位&而不是逻辑&&getIntentData()

于 2012-12-12T00:45:59.897 回答