使用Intent发送嵌套Parcelable

时间:2014-07-23 11:31:32

标签: android parcelable

我正在尝试发送一个Parcelable对象,该对象还包含另一个带有Intent的Parcelable对象。但是,我得到了NullPointer异常。你能告诉我我做错了吗?

A.java

public class A  implements Parcelable {

    private ArrayList<B> var;

    public A()
    {
        this.var = new ArrayList<B>();  
    }

    public void addToB(B b)
    {
        var.add(b);
    }


    public ArrayList<B> getB() {
        return var;
    }

    public void setB(ArrayList<B> b) {
        this.var = b;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeTypedList(this.var);

    }

    private A (Parcel in){

        in.readTypedList(this.var, B.CREATOR);
    }

    public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
        public A createFromParcel(Parcel in) {
            return new A(in);
        }

        public A[] newArray(int size) {
            return new A[size];
        }
    };

}

B.java

public class B implements Parcelable  {

    private String type;


    public B(String type)
    {
        this.type=type;
    }

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }


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

    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeString(this.type);        
    }

    private B(Parcel in) {
        this.type=in.readString();
    }

    public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {
        public B createFromParcel(Parcel in) {
            return new B(in);
        }

        public B[] newArray(int size) {
            return new B[size];
        }
    };

}

我用这样的Intent发送A对象:

Intent i = new Intent(Bla.this, Blabla.class);
            i.putExtra("info", objectA);
            startActivity(i);

我收到了这样的parcelable:

Intent i = getIntent();
        ObjectA ci = (ObjectA)i.getParcelableExtra("info");

3 个答案:

答案 0 :(得分:3)

你必须实现你的ArrayList&lt; B>在:

 private A (Parcel in){
    var = new ArrayList<B>();
    in.readTypedList(this.var, B.CREATOR);
}

说它是否有效:)

答案 1 :(得分:2)

您没有在构造函数中为var变量分配数据。

改变它

private A (Parcel in){

    var=(ArrayList)in.readTypedList(this.var, B.CREATOR);
}

修改::

像这样更改您的代码并尝试

@Override
public void writeToParcel(Parcel dest, int flags) {

    B[] data = new B[var.size()];
    for (int i = 0; i < data.length; i++) {
        data[i] = var.get(i);
    }
    dest.writeParcelableArray(data, flags);

}

public A(Parcel in) {
    Parcelable[] parcelables = in.readParcelableArray(Thread
            .currentThread().getContextClassLoader());
    ArrayList<B> list = new ArrayList<B>();
    for (Parcelable parcelable : parcelables) {
        list.add((B) parcelable);
    }
    var = list;
}

答案 2 :(得分:1)

  

解析arraylist时遇到问题...执行这些更改..

private A (Parcel in){
        in.readTypedList(this.var, B.CREATOR);
}
  

this.var=in.readArrayList(B.class.getClassLoader());
  

public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(this.var);
}
  

dest.writeList(this.var);