实现包含 List 的 Parcelable 接口的对象会抛出 NullPointerException
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是,当
MyParcelable
对象的创建者调用采用Parcel
(从中重建对象)的私有构造函数时,ArrayList
是仍未初始化,因此null
。现在,方法调用
readTypedList()
尝试将Parcel
的内容写入您指定的ArrayList
,这会引发NullPointerEception< /code> 因为它尚未初始化。
解决方案是在调用该方法之前初始化 ArrayList。
The problem is that when the creator of your
MyParcelable
object calls the private constructor that takes theParcel
from which you reconstruct the object, theArrayList
is still uninitialized thusnull
.Now the method call
readTypedList()
tries to write theParcel
s content to theArrayList
you've specified which throws aNullPointerEception
since it isn't initialized yet.The solution is to initialize the
ArrayList
before calling that method.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);