Sending Nested Parcelable with Intent

2019-07-16 12:33发布

问题:

I am trying to send a Parcelable object which also contains another Parcelable object with Intent. However, I am getting NullPointer Exception. Could you please tell me where I am doing wrong?

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

}

I send the A object with Intent like this:

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

I receive the parcelable like this:

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

回答1:

You have to instanciate your ArrayList< B > in :

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

Say if it works :)



回答2:

You are not assigning data to var variable in constructor.

change it like

private A (Parcel in){

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

Edit::

Change your code like this and try

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


回答3:

you have problem with parsing the arraylist... do these changes..

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

to

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

and

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

to

dest.writeList(this.var);