Skip to content Skip to sidebar Skip to footer

Sending Nested Parcelable With Intent

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

Solution 1:

You have to instanciate your ArrayList< B > in :

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

Say if it works :)

Solution 2:

You are not assigning data to var variable in constructor.

change it like

privateA (Parcel in){

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

Edit::

Change your code like this and try

@Override
publicvoidwriteToParcel(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);

}

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

Solution 3:

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

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

to

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

and

publicvoidwriteToParcel(Parcel dest, int flags) {
        dest.writeTypedList(this.var);
}

to

dest.writeList(this.var);

Post a Comment for "Sending Nested Parcelable With Intent"