我想創建A類Parcelable類。如何使用嵌套對象創建類Parcelable
public class A {
public String str;
public ArrayList<B> list;
}
這是我想出這麼遠。但是它會因NullPointerException而崩潰。問題是這兩個陳述:dest.writeList(list);
& in.readList(list, this.getClass().getClassLoader());
。 我想不出什麼從這裏做:(
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];
}
};
}
謝謝您的時間。
標記你回答所接受! –
啊,是的,它不會讓我,然後我有點忘了它:) –
@Snæbjørn感謝分享解決方案 –