我一直在尝试调试此可包裹对象中发生的内存不足错误。
我真的很坚持这一点。我检查了订单,对我来说似乎正确。
import java.util.ArrayList;
import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;
import com.intuch.interfaces.ListRow;
public class ProfileModel implements Parcelable{
public String linkedinID;
public String firstName;
public String lastName;
public String email;
public String pictureURL;
public String headline;
public String industry;
public String interests;
public List<EducationModel> education = new ArrayList<EducationModel>();
public List<PositionModel> position = new ArrayList<PositionModel>();
public boolean introduceOthers;
public boolean answerQuestions;
public boolean beMentor;
public boolean graduated;
public ProfileModel () {
education = new ArrayList<EducationModel>();
position = new ArrayList<PositionModel>();
}
public ProfileModel (Parcel in) {
this();
readFromParcel(in);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(linkedinID);
dest.writeString(firstName);
dest.writeString(lastName);
dest.writeString(email);
dest.writeString(pictureURL);
dest.writeString(headline);
dest.writeString(industry);
dest.writeString(interests);
dest.writeList(education);
dest.writeList(position);
boolean[] myBooleanArr = new boolean[4];
myBooleanArr[0]=introduceOthers;
myBooleanArr[1]=answerQuestions;
myBooleanArr[2]=beMentor;
myBooleanArr[3]=graduated;
dest.writeBooleanArray(myBooleanArr);
}
public static final Parcelable.Creator<ProfileModel> CREATOR = new Parcelable.Creator<ProfileModel>() {
public ProfileModel createFromParcel(Parcel source) {
return new ProfileModel(source);
}
public ProfileModel[] newArray(int size) {
return new ProfileModel[size];
}
};
/*
* Constructor calls read to create object
*/
private void readFromParcel(Parcel in) {
this.linkedinID = in.readString();
this.firstName = in.readString();
this.lastName = in.readString();
this.email = in.readString();
this.pictureURL = in.readString();
this.headline = in.readString();
this.industry = in.readString();
this.interests = in.readString();
in.readTypedList(education, EducationModel.CREATOR);
in.readTypedList(position, PositionModel.CREATOR);
boolean[] myBooleanArr = new boolean[4];
in.readBooleanArray(myBooleanArr);
introduceOthers=myBooleanArr[0];
answerQuestions=myBooleanArr[1];
beMentor=myBooleanArr[2];
graduated=myBooleanArr[3];
}
}
该错误在以下行中发生:
in.readTypedList(position, PositionModel.CREATOR);
这是我的其他 class :
教育模式:
import android.os.Parcel;
import android.os.Parcelable;
public class EducationModel implements Parcelable{
public String degree;
public String schoolName;
public int graduated;
private EducationModel(Parcel in) {
degree = in.readString();
schoolName = in.readString();
graduated = in.readInt();
}
public EducationModel() {
// TODO Auto-generated constructor stub
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(degree);
dest.writeString(schoolName);
dest.writeInt(graduated);
}
public static final Parcelable.Creator<EducationModel> CREATOR = new Parcelable.Creator<EducationModel>() {
public EducationModel createFromParcel(Parcel in) {
return new EducationModel(in);
}
public EducationModel[] newArray(int size) {
return new EducationModel[size];
}
};
}
PositionModel:
import android.os.Parcel;
import android.os.Parcelable;
public class PositionModel implements Parcelable{
public String company;
public String title;
private PositionModel(Parcel in) {
company = in.readString();
title = in.readString();
}
public PositionModel() {
// TODO Auto-generated constructor stub
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(company);
dest.writeString(title);
}
public static final Parcelable.Creator<PositionModel> CREATOR = new Parcelable.Creator<PositionModel>() {
public PositionModel createFromParcel(Parcel in) {
return new PositionModel(in);
}
public PositionModel[] newArray(int size) {
return new PositionModel[size];
}
};
}
最佳答案
您使用通用的writeList
方法将列表写出,但使用readTypedList
调用将其读回。这些不兼容。检查文档readTypedList
:
该列表必须事先通过具有相同对象类型的writeTypedList(List)写入。
使用类型列表可以使Parcel
更加有效地使用内存,因为它不会将列表中每个对象的类型写入流中。因此,如果您尝试回读列表数据,就像它是用“类型化”写入的,而是写为通用列表一样,则包裹处理将获取意外的数据并导致其抛出。