在Android中为类编写Parcelable的StackOverflowError

时间:2017-08-14 09:19:50

标签: java android parcelable serializable

我有一个名为Student的班级。在这个类中,我有一个相同类的Object字段(即Student类)。当我将此类对象作为Parcelable发送到我的应用程序的另一个组件时。它抛出StackOverflowError。

所以我的问题是我如何制作包含相同类字段的类似类的Parcelable。 这是我的代码

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

1 个答案:

答案 0 :(得分:1)

删除dest.writeParcelable(parentChapter, flags);

添加

chapterList = in.createTypedArrayList(Chapter.CREATOR);

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