我想作A类Parcelable。
I'd like to make class A Parcelable.
public class A {
public String str;
public ArrayList<B> list;
}
这是我想出来为止。然而,它崩溃了NullPointerException异常。问题是这两个语句: dest.writeList(名单);
&放大器; in.readList(列表,this.getClass()getClassLoader());
。
我想不出有什么从这里做的:(
This is what I've come up with so far. However it crashes with a NullPointerException. The problem are these two statements: dest.writeList(list);
& in.readList(list, this.getClass().getClassLoader());
.
I can't figure out what to do from here :(
A类
public class A implements Parcelable {
public String str;
public ArrayList<B> list;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(str);
dest.writeList(list);
}
private A(Parcel in) {
str = in.readString();
in.readList(list, this.getClass().getClassLoader());
}
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类
public class B implements Parcelable {
public String str;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(str);
}
private B(Parcel in) {
str = 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];
}
};
}
感谢您的时间。
我终于想通了什么类型到谷歌:),并发现该Android,如何在Parcelable类正确使用readTypedList方法?
I finally figured out what to type into Google :), and found this Android, How to use readTypedList method correctly in a Parcelable class?
该解决方案是使用读/ writeTypedList
代替。我也初始化数组列表,以避免任何进一步的NullPointerException异常。
The solution was to use read-/writeTypedList
instead. I also initialize the arraylist to avoid any further NullPointerException.
A类
public class A implements Parcelable {
public String str;
public ArrayList<B> list = new ArrayList<B>();
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(str);
dest.writeTypedList(list);
}
private A(Parcel in) {
str = in.readString();
in.readTypedList(list, 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类
public class B implements Parcelable {
public String str;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(str);
}
private B(Parcel in) {
str = 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];
}
};
}
上一篇:ActionBarActivity与ActionBarDrawerToggle - 不使用drawerImageResActionBarActivity、ActionBarDrawerToggle、