使用自定义包裹对象在 Arraylist 上解组异常



我正在为数据数组(来自json)对象创建DTO,并实现可包裹以将DTO的arraylist发送到另一个类,但我面临

在偏移量 476 处解组未知类型代码7471216

此问题

.我认为列表答案读\写包裹存在一些问题。我找不到错误。 我像intent.putParcelableArrayListExtra("xxxxx", MyList);一样发送我的数组列表,但在获取它的另一个活动时,我收到了上述错误。请帮忙。

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
}     

包裹类:

 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);
}

最新更新