使用自定义 Parcelable 对象在 Arraylist 上解组异常
Unmarshalling exception on Arraylist with a custom Parcelable Object
我正在为数据数组(来自 json)对象创建 DTO 并实现 parcelable 以将 DTO 的数组列表发送到另一个 class,但我面临
Unmarshalling unknown type code 7471216 at offset 476 this issue
。我认为 List answers read\write parcelling 存在一些问题。我找不到错误。我正在像 intent.putParcelableArrayListExtra("xxxxx", MyList);
一样发送我的数组列表,但是在获取另一个 activity 时,我收到了上述错误。请帮忙。
response{
"errorMessage": null,
"status": true,
"data": [
{
"id": "xxxx",
"question": "Why is your xxxx?",
"isMandatory": true,
"typeOfQuestion": "OPEN",
"answers": [
],
"active": true
},
{
"id": "xxxxx",
"question": "what is your name",
"isMandatory": true,
"typeOfQuestion": "OPEN",
"answers": [
],
"active": true
}
],
"httpStatus": 200
}
可包裹 Class:
public class QuestionAnswerDto implements Parcelable {
private String id;
private String question;
private String isMandatory;
private String typeOfQuestion;
private String active;
private List<String> answers;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getIsMandatory() {
return isMandatory;
}
public void setIsMandatory(String isMandatory) {
this.isMandatory = isMandatory;
}
public String getTypeOfQuestion() {
return typeOfQuestion;
}
public void setTypeOfQuestion(String typeOfQuestion) {
this.typeOfQuestion = typeOfQuestion;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
public List<String> getAnswers() {
return answers;
}
public void setAnswers(List<String> answers) {
this.answers = answers;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.question);
dest.writeString(this.typeOfQuestion);
dest.writeString(this.active);
dest.writeString(this.isMandatory);
dest.writeStringList(this.answers);
}
protected QuestionAnswerDto(Parcel in) {
this.id = in.readString();
this.question = in.readString();
this.typeOfQuestion = in.readString();
this.active = in.readString();
this.answers = new ArrayList<>();
in.readStringList(this.answers);
this.isMandatory = in.readString();
}
public static final Creator<QuestionAnswerDto> CREATOR = new Creator<QuestionAnswerDto>() {
public QuestionAnswerDto createFromParcel(Parcel source) {
return new QuestionAnswerDto(source);
}
public QuestionAnswerDto[] newArray(int size) {
return new QuestionAnswerDto[size];
}
};
}
您输入和阅读的顺序不正确。使用 Parcels
序列化和反序列化对象时,您 必须 以与写入对象相同的顺序读取。
请参阅下面更正后的代码(我移动了 QuestionAnswerDto
构造函数中的最后一行):
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.question);
dest.writeString(this.typeOfQuestion);
dest.writeString(this.active);
dest.writeString(this.isMandatory);
dest.writeStringList(this.answers);
}
protected QuestionAnswerDto(Parcel in) {
this.id = in.readString();
this.question = in.readString();
this.typeOfQuestion = in.readString();
this.active = in.readString();
this.isMandatory = in.readString();
this.answers = new ArrayList<>();
in.readStringList(this.answers);
}
我正在为数据数组(来自 json)对象创建 DTO 并实现 parcelable 以将 DTO 的数组列表发送到另一个 class,但我面临
Unmarshalling unknown type code 7471216 at offset 476 this issue
。我认为 List answers read\write parcelling 存在一些问题。我找不到错误。我正在像 intent.putParcelableArrayListExtra("xxxxx", MyList);
一样发送我的数组列表,但是在获取另一个 activity 时,我收到了上述错误。请帮忙。
response{
"errorMessage": null,
"status": true,
"data": [
{
"id": "xxxx",
"question": "Why is your xxxx?",
"isMandatory": true,
"typeOfQuestion": "OPEN",
"answers": [
],
"active": true
},
{
"id": "xxxxx",
"question": "what is your name",
"isMandatory": true,
"typeOfQuestion": "OPEN",
"answers": [
],
"active": true
}
],
"httpStatus": 200
}
可包裹 Class:
public class QuestionAnswerDto implements Parcelable {
private String id;
private String question;
private String isMandatory;
private String typeOfQuestion;
private String active;
private List<String> answers;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getIsMandatory() {
return isMandatory;
}
public void setIsMandatory(String isMandatory) {
this.isMandatory = isMandatory;
}
public String getTypeOfQuestion() {
return typeOfQuestion;
}
public void setTypeOfQuestion(String typeOfQuestion) {
this.typeOfQuestion = typeOfQuestion;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
public List<String> getAnswers() {
return answers;
}
public void setAnswers(List<String> answers) {
this.answers = answers;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.question);
dest.writeString(this.typeOfQuestion);
dest.writeString(this.active);
dest.writeString(this.isMandatory);
dest.writeStringList(this.answers);
}
protected QuestionAnswerDto(Parcel in) {
this.id = in.readString();
this.question = in.readString();
this.typeOfQuestion = in.readString();
this.active = in.readString();
this.answers = new ArrayList<>();
in.readStringList(this.answers);
this.isMandatory = in.readString();
}
public static final Creator<QuestionAnswerDto> CREATOR = new Creator<QuestionAnswerDto>() {
public QuestionAnswerDto createFromParcel(Parcel source) {
return new QuestionAnswerDto(source);
}
public QuestionAnswerDto[] newArray(int size) {
return new QuestionAnswerDto[size];
}
};
}
您输入和阅读的顺序不正确。使用 Parcels
序列化和反序列化对象时,您 必须 以与写入对象相同的顺序读取。
请参阅下面更正后的代码(我移动了 QuestionAnswerDto
构造函数中的最后一行):
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.id);
dest.writeString(this.question);
dest.writeString(this.typeOfQuestion);
dest.writeString(this.active);
dest.writeString(this.isMandatory);
dest.writeStringList(this.answers);
}
protected QuestionAnswerDto(Parcel in) {
this.id = in.readString();
this.question = in.readString();
this.typeOfQuestion = in.readString();
this.active = in.readString();
this.isMandatory = in.readString();
this.answers = new ArrayList<>();
in.readStringList(this.answers);
}