开发者

Object implementing the Parcelable interface containing a List throws a NullPointerException

开发者 https://www.devze.com 2023-03-25 05:49 出处:网络
I am trying to make an object containing a List of objects parcelable using the Parcelable interface. I am not able to read the Parcel object back in.

I am trying to make an object containing a List of objects parcelable using the Parcelable interface. I am not able to read the Parcel object back in.

Can anyone point me in the right direction? What am I missing here?

MyParcelable object:

public class MyParcelable implements Parcelable {

    private int myInt = 0;
    private List<MyListClass> arrList;

    public List<MyListClass> getArrList() {
        return arrList;
    }

    public void setArrList(List<MyListClass> arrList) {
        this.arrList = arrList;
    }

    public int getMyInt() {
        return myInt;
    }

    public void setMyInt(int myInt) {
        this.myInt = myInt;
    }

    MyParcelable() {
        // initialization
        arrList = new ArrayList<MyListClass>();
    }

    public MyParcelable(Parcel in) {
        myInt = in.readInt();
        in.readTypedList(arrList, MyListClass.CREATOR);
    }

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

    @Override
    public void writeToParcel(Parcel outParcel, int flags) {
        outParcel.writeInt(myInt);
        outParcel.writeTypedList(arrList);
    }

    public static final Parcelable.Creator<MyParcelable> CREATOR =
            new Parcelable.Creator<MyParcelable>() {

        @Override
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        @Override
        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };
}

MyListClass object:

  public class MyListClass implements Parcelable{

    private int te开发者_JAVA技巧st;

    public MyListClass()
    {}

    public MyListClass(Parcel read){
        test = read.readInt();
    }

    public int getTest() {
        return test;
    }

    public void setTest(int test) {
        this.test = test;
    }

    public static final Parcelable.Creator<MyListClass> CREATOR = 
        new Parcelable.Creator<MyListClass>() {

            @Override
            public MyListClass createFromParcel(Parcel source) {
                return new MyListClass(source);
            }

            @Override
            public MyListClass[] newArray(int size) {
                return new MyListClass[size];
            }
        };

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

    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        arg0.writeInt(test);
    }
}


The problem is that when the creator of your MyParcelable object calls the private constructor that takes the Parcel from which you reconstruct the object, the ArrayList is still uninitialized thus null.

Now the method call readTypedList() tries to write the Parcels content to the ArrayList you've specified which throws a NullPointerEception since it isn't initialized yet.

The solution is to initialize the ArrayList before calling that method.

public MyParcelable(Parcel in) {
    myInt = in.readInt();
    arrList = new ArrayList<MyListClass>();
    in.readTypedList(arrList, MyListClass.CREATOR);
}


in.readTypedList(arrList, MyListClass.CREATOR);

arrList at this point has not been initialised when you call return new MyParcelable(in);

0

精彩评论

暂无评论...
验证码 换一张
取 消