Parcel在读取自定义类列表时解组未知类型代码

时间:2014-09-02 17:14:45

标签: android parcelable parcel

我试图将自定义类实现为parcelable,并且我在从包裹中读取/写入这些类的列表时遇到问题。我有3个班,测验,问答。

在问题类下面给我一个错误:

 in.readList(answers, Answer.class.getClassLoader());

这是我的班级, 测验:

public class Quiz implements Parcelable{
    private List<Question> questions;

    public Quiz(Parcel in) {
        questions = new ArrayList<Question>();
        in.readList(questions,Question.class.getClassLoader());
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeList(questions);
    }

    public static final Parcelable.Creator<Quiz> CREATOR = new Parcelable.Creator<Quiz>() {

        public Quiz createFromParcel(Parcel in) {
            return new Quiz(in);
        }

        public Quiz[] newArray(int size) {
            return new Quiz[size];
        }
    };
}

问题:

public class Question implements Parcelable {

    private String question;
    private List<Answer> answers;

    public Question(Parcel in) {
        answers = new ArrayList<Answer>();
        in.readList(answers, Answer.class.getClassLoader());
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(question);
        parcel.writeList(answers);
    }

    public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {

        public Question createFromParcel(Parcel in) {
            return new Question(in);
        }

        public Question[] newArray(int size) {
            return new Question[size];
        }
    };
}

答案:

public class Answer implements Parcelable {
    private String answer;
    private boolean isCorrect = false;

    public Answer(String answer) {
        this.answer = answer;
    }

    public Answer(Parcel in) {
       answer =  in.readString();
       isCorrect = in.readByte() != 0;
    }

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(answer);
        parcel.writeByte((byte) (isCorrect ? 1 : 0));
    }
    public static final Parcelable.Creator<Answer> CREATOR = new Parcelable.Creator<Answer>() {

        public Answer createFromParcel(Parcel in) {
            return new Answer(in);
        }

        public Answer[] newArray(int size) {
            return new Answer[size];
        }
    };
}

堆栈跟踪:

   09-03 01:03:43.018  20659-20659/auf.ccs.appschool E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{auf.ccs.appschool/auf.ccs.appschool.QuizActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@40516de0: Unmarshalling unknown type code 6815831 at offset 332
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
                at android.app.ActivityThread.access$1500(ActivityThread.java:117)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:130)
                at android.app.ActivityThread.main(ActivityThread.java:3691)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:507)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@40516de0: Unmarshalling unknown type code 6815831 at offset 332
                at android.os.Parcel.readValue(Parcel.java:1913)
                at android.os.Parcel.readListInternal(Parcel.java:2092)
                at android.os.Parcel.readList(Parcel.java:1414)
                at auf.ccs.appschool.Items.Question.<init>(Question.java:25)
                at auf.ccs.appschool.Items.Question$1.createFromParcel(Question.java:67)
                at auf.ccs.appschool.Items.Question$1.createFromParcel(Question.java:64)
                at android.os.Parcel.readParcelable(Parcel.java:1981)
                at android.os.Parcel.readValue(Parcel.java:1846)
                at android.os.Parcel.readListInternal(Parcel.java:2092)
                at android.os.Parcel.readList(Parcel.java:1414)
                at auf.ccs.appschool.Items.Quiz.<init>(Quiz.java:28)
                at auf.ccs.appschool.Items.Quiz$1.createFromParcel(Quiz.java:44)
                at auf.ccs.appschool.Items.Quiz$1.createFromParcel(Quiz.java:41)
                at android.os.Parcel.readParcelable(Parcel.java:1981)
                at auf.ccs.appschool.Items.Video.<init>(Video.java:113)
                at auf.ccs.appschool.Items.Video$1.createFromParcel(Video.java:132)
                at auf.ccs.appschool.Items.Video$1.createFromParcel(Video.java:130)
                at android.os.Parcel.readParcelable(Parcel.java:1981)
                at android.os.Parcel.readValue(Parcel.java:1846)
                at android.os.Parcel.readMapInternal(Parcel.java:2083)
                at android.os.Bundle.unparcel(Bundle.java:208)
                at android.os.Bundle.getParcelable(Bundle.java:1100)
                at auf.ccs.appschool.QuizActivity.onCreate(QuizActivity.java:40)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
                at android.app.ActivityThread.access$1500(ActivityThread.java:117)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:130)
                at android.app.ActivityThread.main(ActivityThread.java:3691)
                at java.lang.reflect.Method.invokeNative(Native Method)

1 个答案:

答案 0 :(得分:2)

在您正在阅读的Question课程中

answers = new ArrayList<Answer>();
in.readList(answers, Answer.class.getClassLoader());

来自parcel,但正在写

parcel.writeString(question);
parcel.writeList(answers);
进入它。小心以相同的顺序在包裹中写入和读取相同的值(类型)。

已修复Question构造函数:

public Question(Parcel in) {
    question = in.readString();
    answers = new ArrayList<Answer>();
    in.readList(answers, Answer.class.getClassLoader());
}