java - 在 android 中为类编写 Parcelable 时出现 StackOverflowError

标签 java android parcelable serializable

我有一个名为“学生”的类(class)。在这个类(class)中,我有一个同一类(class)(即学生类(class))的对象字段。当我将此类对象作为 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);
    }
}

最佳答案

删除dest.writeParcelable(parentChapter, flags);

添加

chapterList = in.createTypedArrayList(Chapter.CREATOR);

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

关于java - 在 android 中为类编写 Parcelable 时出现 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45671248/

相关文章:

java - Maven过滤乱码特殊字符

java - JFR:读取 .jfr 文件时出现 OutOfMemoryError

java - 在数组适配器类中设置 TextView 的值时出现空指针异常 - Android

Android 操作栏 : show/hide tabs dynamically?

java - 如何在Intent中发送模型的对象

java - 使用 hibernate sessionFactory 或 JPA entityManager?

java - Hibernate @email 不关心域或 FQDN

android - 什么时候可以获得 Activity 中的 View 维度?

android - Monodroid 将可打包对象打包

安卓|在 bundle 中使用 Parcelable 数据时解码未知类型