Unmarshalling exception on Arraylist with a custom Parcelable Object

191 views Asked by At

I am creating DTO for data array(from json) object and implementing parcelable to send arraylist of DTO to another class , but I am facing

Unmarshalling unknown type code 7471216 at offset 476 this issue

. I think there is some problem with List answers read\write parcelling . I am unable to find the bug. I am sending my arraylist like intent.putParcelableArrayListExtra("xxxxx", MyList); but on fetching it another activity I am receiving the above mentioned error. Please help.

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

There are 1 answers

0
Rolf ツ On BEST ANSWER

The order you are writing and reading in is not correct. When serializing and deserializing objects using Parcels you MUST read in the same order as you have written the objects.

See the corrected code below (I moved the last line in the QuestionAnswerDto constructor):

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