使用自定义Parcelable对象解组Arraylist上的异常

时间:2016-12-30 09:13:43

标签: android arraylist parcelable parcel

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

  

在本期错误476处解组未知类型代码7471216

。我认为列表答案读取\写入parcelling存在一些问题。我无法找到错误。我正在发送像intent.putParcelableArrayListExtra("xxxxx", MyList);这样的arraylist,但在获取另一项活动时我收到了上述错误。请帮忙。

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
}     

Parcelable 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];
    }
  };

  }

1 个答案:

答案 0 :(得分:3)

您正在撰写和阅读的订单不正确。使用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);
}