简体   繁体   中英

StackOverflowError on writing Parcelable for a class in android

I have a class named Student. In this class I have a Object field of same class(ie of Student class). When I am sending this classes object as Parcelable to another component of my app. It throws StackOverflowError.

So my question is how can I make Parcelable of such kind of classes who contains fields of same class. Here is my code

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;

public class Chapter implements Parcelable{
    private String chapterId;
    private String chapterNumber;
    private String chapterName;
    private String src;
    private int startTime;
    private int endTime;
    private Chapter parentChapter;
    private ArrayList<Chapter> chapterList;


    public Chapter(){}

    protected Chapter(Parcel in) {
        chapterId = in.readString();
        chapterNumber = in.readString();
        chapterName = in.readString();
        src = in.readString();
        startTime = in.readInt();
        endTime = in.readInt();
        parentChapter = in.readParcelable(Chapter.class.getClassLoader());
        chapterList = in.createTypedArrayList(Chapter.CREATOR);
    }

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

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

    String getChapterName() {
        return chapterName;
    }

    void setChapterName(String chapterName) {
        this.chapterName = chapterName;
    }

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }

    void setChapterNumber(String chapterNumber) {
        this.chapterNumber = chapterNumber;
    }

    void setChapterId(String chapterId) {
        this.chapterId = chapterId;
    }

    ArrayList<Chapter> getChapterList() {
        return chapterList;
    }

    void setChapterList(ArrayList<Chapter> chapterList) {
        this.chapterList = chapterList;
    }

    public int getStartTime() {
        return startTime;
    }

    public void setStartTime(int startTime) {
        this.startTime = startTime;
    }

    void setEndTime(int endTime) {
        this.endTime = endTime;
    }

    public Chapter getParentChapter() {
        return parentChapter;
    }

    public void setParentChapter(Chapter parentChapter) {
        this.parentChapter = parentChapter;
    }


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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(chapterId);
        dest.writeString(chapterNumber);
        dest.writeString(chapterName);
        dest.writeString(src);
        dest.writeInt(startTime);
        dest.writeInt(endTime);
        dest.writeParcelable(parentChapter, flags);
        dest.writeTypedList(chapterList);
    }
}

Remove dest.writeParcelable(parentChapter, flags);

Add

chapterList = in.createTypedArrayList(Chapter.CREATOR);

for( Chapter c : chapterList ) {
  c.setParentChapter(this);
}

if parentChapter == this ,will cause this problem。

  1. try check logic setParentChapter
  2. or use this code ignore
if (parentChapter == this) { // Caused by: java.lang.StackOverflowError
    dest.writeParcelable(null, 0);
} else {
    dest.writeParcelable(parentChapter, 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM