我一直在尝试调试在我的parcelable对象中发生的内存不足错误。 我真的坚持这个。我检查了订单,这对我来说似乎是对的。
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);
以下是我的其他课程:
EducationalModel:
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];
}
};
}
答案 0 :(得分:2)
您使用通用writeList
方法编写列表,但使用readTypedList
调用将其读回。这些不兼容。查看readTypedList
的文档:
The list must have previously been written via writeTypedList(List) with the same object type.
使用类型化列表可以使Parcel
具有更高效的内存使用率,因为它不会将列表中每个对象的类型写入流中。因此,如果您尝试将列表数据读回,就好像它是以“键入”的形式写入,而是写为通用列表,则包裹处理会获得意外数据并导致其丢失。