实现包含 List 的 Parcelable 接口的对象会抛出 NullPointerException

发布于 2024-11-28 04:21:33 字数 2478 浏览 1 评论 0原文

我正在尝试使用 Parcelable 接口创建一个包含对象 List 的对象。我无法重新读取 Parcel 对象。

有人能给我指出正确的方向吗?我在这里缺少什么?

MyParcelable 对象:

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 对象:

  public class MyListClass implements Parcelable{

    private int test;

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

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 test;

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野稚 2024-12-05 04:21:33

问题是,当 MyParcelable 对象的创建者调用采用 Parcel(从中重建对象)的私有构造函数时,ArrayList 是仍未初始化,因此 null

现在,方法调用 readTypedList() 尝试将 Parcel 的内容写入您指定的 ArrayList,这会引发 NullPointerEception< /code> 因为它尚未初始化。

解决方案是在调用该方法之前初始化 ArrayList。

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

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);
}
赢得她心 2024-12-05 04:21:33

in.readTypedList(arrList, MyListClass.CREATOR);

当调用return new MyParcelable(in);时,此时arrList尚未初始化

in.readTypedList(arrList, MyListClass.CREATOR);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文