Android 我在这门包裹课上做错了什么?
我正在尝试启动一个新活动,并将一个自定义的可包裹对象添加到目标中。 显然,这是行不通的: 类别:Android 我在这门包裹课上做错了什么?,android,parcelable,Android,Parcelable,我正在尝试启动一个新活动,并将一个自定义的可包裹对象添加到目标中。 显然,这是行不通的: 类别: public class TestObject implements Parcelable{ private String mString; private int mInt; private List<String> mList; public TestObject() { this.mString = "This is a String"; this.mInt
public class TestObject implements Parcelable{
private String mString;
private int mInt;
private List<String> mList;
public TestObject() {
this.mString = "This is a String";
this.mInt = 777;
mList = new ArrayList<String>();
for(int i=0; i<100; i++){
mList.add(String.valueOf(i));
}
}
public TestObject(Parcel in) {
setmInt(in.readInt());
setmString(in.readString());
mList = new ArrayList<String>();
in.readStringList(mList);
}
public void setmString(String mString) {
this.mString = mString;
}
public void setmInt(int mInt) {
this.mInt = mInt;
}
public String getmString() {
return mString;
}
public int getmInt() {
return mInt;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mString);
dest.writeInt(mInt);
dest.writeStringList(mList);
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public TestObject createFromParcel(Parcel in) {
return new TestObject(in);
}
public TestObject[] newArray(int size) {
return new TestObject[size];
}
};
第二项活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
TestObject ob = (TestObject) i.getParcelableExtra("object");
Debug.stopMethodTracing();
Log.d("object string", "string: " + ob.getmString());
}
问题在于
列表
..问题在于,将变量写入包裹的顺序与读取变量的顺序不同。你先写mString,但先读mInt。这将有助于:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mString);
dest.writeInt(mInt);
dest.writeStringList(mList);
}
public TestObject(Parcel in) {
setmString(in.readString());
setmInt(in.readInt());
mList = new ArrayList<String>();
in.readStringList(mList);
}
@覆盖
公共无效写入包裹(包裹目的地,内部标志){
目的写入字符串(mString);
目的地记录(造币厂);
目的写入列表(mList);
}
公共测试对象(地块中){
setmString(in.readString());
setmInt(in.readInt());
mList=新的ArrayList();
in.readStringList(mList);
}
顺便说一句,不要为创建者使用原始类型,而是使用以下类型:
public static final Parcelable.Creator<TestObject> CREATOR = new Parcelable.Creator<TestObject>() {
public TestObject createFromParcel(Parcel in) {
return new TestObject(in);
}
public TestObject[] newArray(int size) {
return new TestObject[size];
}
};
public static final Parcelable.Creator=新的Parcelable.Creator(){
公共测试对象createFromParcel(中的地块){
返回新的TestObject(in);
}
公共TestObject[]新数组(整数大小){
返回新的TestObject[size];
}
};
问题在于,将变量写入包裹的顺序与读取顺序不同。你先写mString,但先读mInt。这将有助于:
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mString);
dest.writeInt(mInt);
dest.writeStringList(mList);
}
public TestObject(Parcel in) {
setmString(in.readString());
setmInt(in.readInt());
mList = new ArrayList<String>();
in.readStringList(mList);
}
@覆盖
公共无效写入包裹(包裹目的地,内部标志){
目的写入字符串(mString);
目的地记录(造币厂);
目的写入列表(mList);
}
公共测试对象(地块中){
setmString(in.readString());
setmInt(in.readInt());
mList=新的ArrayList();
in.readStringList(mList);
}
顺便说一句,不要为创建者使用原始类型,而是使用以下类型:
public static final Parcelable.Creator<TestObject> CREATOR = new Parcelable.Creator<TestObject>() {
public TestObject createFromParcel(Parcel in) {
return new TestObject(in);
}
public TestObject[] newArray(int size) {
return new TestObject[size];
}
};
public static final Parcelable.Creator=新的Parcelable.Creator(){
公共测试对象createFromParcel(中的地块){
返回新的TestObject(in);
}
公共TestObject[]新数组(整数大小){
返回新的TestObject[size];
}
};