Louckas
Louckas

Reputation: 21

Nested parcelable runtime execption

I am working on an Android app where I create an ArrayList of Parcelable object that contains an ArrayList of Parcelable object. To fit my case, it's a public ArrayList<FolderModel> folderList_;

My goal is to pass this ArrayList through an Intent, hence the Parcelable implementation, my problem arise when I try to get the data out of the Intent where I get this error code:

Parcel android.os.Parcel@9aa641d: Unmarshalling unknown type code 6881389 at offset 1696. 

I can't really make sense out of it. I looked around but couldn't find anything related to my issue.

Another funny behavior is that, if I comment out the folderName_ inside my ExerciseModel in public void writeToParcel then it works. Can anyone help me figure out this mystery?

As far as I could see, the two classes are implemented properly as Parcelable object, I will continue to look for an answer, but I feel like I have reached a wall.

After further testing, I can safely transmit an ArrayList of ExerciseModel which would point me in the direction that the problem happens either in FolderModel or in the ArrayList in this class.

All the code can be browsed at https://github.com/louckas/TrainingPlan, I will only share the relevant class below.

FolderModel class:

public class FolderModel implements Parcelable {
    String folderName_ = "Folder";
    Boolean extended_ = false;
    ArrayList<ExerciseModel> exerciseList_;

    public FolderModel(String folderName, Boolean extended, ArrayList<ExerciseModel> exerciseList) {
        folderName_ = folderName;
        extended_ = extended;
        exerciseList_ = exerciseList;
    }

    protected FolderModel(Parcel in) {
        folderName_ = in.readString();
        byte tmpExtended_ = in.readByte();
        extended_ = tmpExtended_ == 0 ? null : tmpExtended_ == 1;
        exerciseList_ = in.createTypedArrayList(ExerciseModel.CREATOR);
    }

    public static final Creator<FolderModel> CREATOR = new Creator<FolderModel>() {
        @Override
        public FolderModel createFromParcel(Parcel in) {
            return new FolderModel(in);
        }

        @Override
        public FolderModel[] newArray(int size) {
            return new FolderModel[size];
        }
    };

    public String getFolderName_() {
        return folderName_;
    }

    public Boolean getExtended_() {
        return extended_;
    }

    public void setExtended_(boolean extended) { extended_ = extended; }

    public List<ExerciseModel> getExerciseList_() { return exerciseList_; }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(folderName_);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            parcel.writeParcelableList(exerciseList_, i);
        }
    }
}

ExerciseModel class:

public class ExerciseModel  implements Parcelable {
    String exerciseName_;
    String description_;
    String folderName_;

    public ExerciseModel(String exerciseName, String description, String folderName) {
        exerciseName_ = exerciseName;
        description_ = description;
        folderName_ = folderName;
    }

    protected ExerciseModel(Parcel in) {
        exerciseName_ = in.readString();
        description_ = in.readString();
        folderName_ = in.readString();
    }

    public static final Creator<ExerciseModel> CREATOR = new Creator<ExerciseModel>() {
        @Override
        public ExerciseModel createFromParcel(Parcel in) {
            return new ExerciseModel(in);
        }

        @Override
        public ExerciseModel[] newArray(int size) {
            return new ExerciseModel[size];
        }
    };

    public String getExerciseName_() { return exerciseName_; }

    public String getDescription_() { return description_; }

    public String getFolderName_() { return folderName_; }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(exerciseName_);
        parcel.writeString(description_);
        parcel.writeString(folderName_);
    }
}

Upvotes: 1

Views: 104

Answers (1)

Louckas
Louckas

Reputation: 21

After looking further into it, I figured out the issue !

The problem was in the FolderModel in the writeToParcel the extended field wasn't put into the Parcelable which mean 2 arguments and in the protected FolderModel(Parcel in) constructor I was reading 3. Therefore, the problem.

Here is the correction I had to do in order to solve my issue:

public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(folderName_);
    parcel.writeByte((byte) (extended_ ? 1 : 0));
    parcel.writeTypedList(exerciseList_);
}

Upvotes: 1

Related Questions