我一直在尝试调试我的 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);
这是我的其他类(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];
}
};
}
位置模型:
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
的文档:
The list must have previously been written via writeTypedList(List) with the same object type.
使用类型化列表允许 Parcel
更有效地使用内存,因为它不会将列表中每个对象的类型写入流中。因此,如果您尝试回读列表数据,就好像它是用“类型化”编写的,但实际上是作为通用列表编写的,包裹处理会获取意外数据并导致它抛出。
关于Android parcelable readtypedlist 内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033283/