传递活动之间的ArrayList(Passing ArrayList between activit

2019-07-05 00:24发布

我有我在哪里对象添加到它实现了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;
}

}

变化编写一个解决的问题:

CHANGED的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();

    }
}

Answer 1:

改写
问题是,你有一个额外的包。 目前在getIntentData()你必须调用:

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

让我们削减了不必要的包。

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");
    ...
}


Answer 2:

除非我错过了什么,你正在使用的按位&代替逻辑&&getIntentData()函数



文章来源: Passing ArrayList between activities