java.lang.RuntimeException:包裹android.os.Parcel:解组未知类型代码 [英] java.lang.RuntimeException: Parcel android.os.Parcel: Unmarshalling unknown type code

查看:59
本文介绍了java.lang.RuntimeException:包裹android.os.Parcel:解组未知类型代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用似乎出现了一个奇怪的错误(请参阅 GitHub ),当我将对象传递给实现Parcelable的不同活动时.

我在Stack Overflow上检查了其他问题和答案,但是找不到解决方案.我尝试在此处回答-例如,此处仅供参考:

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

我还确保了writeToParcel中的方法调用是正确的.关于此问题的Stack Overflow上的其他大多数问题都没有答案.

此外,我问一个新问题的原因是因为我认为我的问题是由于我在应用程序中使用界面的方式引起的(稍后我将对此进行扩展).关于堆栈溢出的其他问题不适合我的特定情况.

下面,我通过GitHub提供了到代码的链接,以便您可以根据需要探索更多代码.


当我单击按钮 Topic 界面 .

但是,Topic接口扩展了Parcelable,因此实现Topic的对象还包括构造函数CREATOR字段,以及实现Parcelable的类通常必须具有的方法./p>

您可以通过GitHub链接查看相关的类,但是我将在下面提供这些类的相关部分.这是Topic界面:

public interface Topic extends Parcelable {

    int getId();

    String getIdentifier();

    String getName();

    Course getCourse();


    ArrayList<FlashCard> getFlashCards(Context context);


    class FlashCardsRetriever {

        public static ArrayList<FlashCard> filterStandardCards(ArrayList<FlashCard> flashCards, @StandardFlashCard.ContentType int contentType) {
            ArrayList<FlashCard> filteredCards = new ArrayList<>();
            for (FlashCard flashCard : flashCards) {
                boolean isPaper2 = ((StandardFlashCard) flashCard).isPaper2();
                boolean condition;
                switch (contentType) {
                    case StandardFlashCard.PAPER_1:
                        condition = !isPaper2;
                        break;
                    case StandardFlashCard.PAPER_2:
                        condition = isPaper2;
                        break;
                    case StandardFlashCard.ALL:
                        condition = true;
                        break;
                    default:
                        throw new IllegalArgumentException("content type '" + contentType + "' is invalid");
                }
                if (condition) filteredCards.add(flashCard);
            }
            return filteredCards;
        }

        ...
    }

}

implements Topic的类(对象):

public class CourseTopic implements Topic {

    ...

    public CourseTopic(int id, String identifier, String name, Course course) {
        ...
    }

    @Override
    public int getId() {
        return mId;
    }

    @Override
    public String getIdentifier() {
        return mIdentifier;
    }

    ...


    protected CourseTopic(Parcel in) {
        mId = in.readInt();
        mIdentifier = in.readString();
        mName = in.readString();
        mCourse = in.readParcelable(Course.class.getClassLoader());
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mId);
        dest.writeString(mIdentifier);
        dest.writeString(mName);
        dest.writeParcelable(mCourse, flags);
    }

}

在上面代码的最后几行之一中,您可以看到我传递了mCourse,这是我创建的Course对象.在这里:

public class Course implements Parcelable {

    ...

    public Course(String subject, String examBoard, @FlashCard.CourseType String courseType,
              String revisionGuide) {
        ...
    }


    public String getSubjectIdentifier() {
        return mSubjectIdentifier;
    }

    public String getExamBoardIdentifier() {
        return mBoardIdentifier;
    }

    public ArrayList<Topic> getTopics(Context context) {
        ArrayList<Topic> topics = new ArrayList<>();
        String filename = mSubjectIdentifier + "_" + mBoardIdentifier + "_topics.csv";
        CsvParser parser = CsvUtils.getMyParser();
        try {
            List<String[]> allRows = parser.parseAll(context.getAssets().open(filename));
            for (String[] line : allRows) {
                int id = Integer.parseInt(line[0]);
                topics.add(new CourseTopic(id, line[1], line[2], this));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return topics;
    }

    ...


    protected Course(Parcel in) {
        mSubjectIdentifier = in.readString();
        mBoardIdentifier = in.readString();
        mCourseType = in.readString();
        mRevisionGuide = in.readString();
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mSubjectIdentifier);
        dest.writeString(mBoardIdentifier);
        dest.writeString(mCourseType);
        dest.writeString(mRevisionGuide);
    }

}

我怀疑这里可能是引起问题的原因,也是我的情况与其他问题不同的原因.


说实话,我不确定是什么原因导致了错误,所以对答案中的解释和指导将不胜感激.


修改:

David Wasser 的建议之后,我已经更新了部分代码,如下所示:

FlashCardActivity.java-onCreate(...):

Bundle extras = getIntent().getExtras();
extras.setClassLoader(Topic.class.getClassLoader());
mTopic = extras.getParcelable(EXTRA_TOPIC);

Course.java-writeToParcel(...):

dest.writeString(mSubjectIdentifier);
dest.writeString(mBoardIdentifier);
dest.writeString(mCourseType);
dest.writeInt(mRevisionGuide == null ? 0 : 1);
if (mRevisionGuide != null) dest.writeString(mRevisionGuide);

Course.java-Course(Parcel in):

mSubjectIdentifier = in.readString();
mBoardIdentifier = in.readString();
mCourseType = in.readString();
if (in.readInt() != 0) mRevisionGuide = in.readString();

我使用Log.d(...)添加了日志消息,以查看在writeToParcel(...)中传递时是否有任何变量为空,并使用 David Wasser 的方法正确处理了此问题.

但是,我仍然收到相同的错误消息.

解决方案

您的问题在LanguagesFlashCard中.这是您的包裹/取消包裹方法:

protected LanguagesFlashCard(Parcel in) {
    mId = in.readInt();
    mEnglish = in.readString();
    mAnswerPrefix = in.readString();
    mAnswer = in.readString();
    mTier = in.readInt();
    mTopic = in.readParcelable(Topic.class.getClassLoader());
}

如您所见,它们不匹配.写入Parcel的第二项是int,从Parcel读取的第二项是String.

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(mId);
    dest.writeInt(mTier);
    dest.writeString(mEnglish);
    dest.writeString(mAnswerPrefix);
    dest.writeString(mAnswer);
    dest.writeParcelable(mTopic, flags);
}

I seem to be getting a strange error in my app (see GitHub), which occurs when I pass objects to different activities that implement Parcelable.

I have checked other questions and answers here on Stack Overflow, but I was unable to find a solution. I've tried the answer here, for example - here it is for reference:

-keepclassmembers class * implements android.os.Parcelable {
    static ** CREATOR;
}

I've also made sure that the method calls in writeToParcel are in order. Most other questions on Stack Overflow about this issue don't have answers.

Moreover, the reason I am asking a new question is because I think my problem is caused because of how I have used interfaces in my app (I will expand on this point later on). Other questions on Stack Overflow would not suit my particular scenario.

In the following, I have provided links to the code via GitHub, so that you can explore more of the code if required.


When I click on a button to launch a new activity (passing an object that implements Parcelable), there is a crash:

Process: com.satsuware.flashcards, PID: 4664
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.satsuware.flashcards/com.satsumasoftware.flashcards.ui.FlashCardActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@d2219e4: Unmarshalling unknown type code 6815860 at offset 200
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    ...
 Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@d2219e4: Unmarshalling unknown type code 6815860 at offset 200
at android.os.Parcel.readValue(Parcel.java:2319)
at android.os.Parcel.readListInternal(Parcel.java:2633)
at android.os.Parcel.readArrayList(Parcel.java:1914)
at android.os.Parcel.readValue(Parcel.java:2264)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2592)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.Bundle.getParcelable(Bundle.java:786)
at android.content.Intent.getParcelableExtra(Intent.java:5377)
at com.satsumasoftware.flashcards.ui.FlashCardActivity.onCreate(FlashCardActivity.java:71)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
...

I call the aforementioned activity like so (also see GitHub):

Intent intent = new Intent(TopicDetailActivity.this, FlashCardActivity.class);
intent.putExtra(FlashCardActivity.EXTRA_TOPIC, mTopic);
intent.putExtra(FlashCardActivity.EXTRA_NUM_CARDS, mSelectedNumCards);
intent.putExtra(FlashCardActivity.EXTRA_CARD_LIST, mFilteredCards);
startActivity(intent);

The main part to consider is when I pass mTopic. This is a Topic interface that I created.

However, the Topic interface extends Parcelable and so the objects that implement Topic also include the constructor, CREATOR field, and the methods that a class implementing Parcelable would normally have to have.

You can view the relevant classes via the GitHub links, but I will provide the relevant parts of these classes below. Here is the Topic interface:

public interface Topic extends Parcelable {

    int getId();

    String getIdentifier();

    String getName();

    Course getCourse();


    ArrayList<FlashCard> getFlashCards(Context context);


    class FlashCardsRetriever {

        public static ArrayList<FlashCard> filterStandardCards(ArrayList<FlashCard> flashCards, @StandardFlashCard.ContentType int contentType) {
            ArrayList<FlashCard> filteredCards = new ArrayList<>();
            for (FlashCard flashCard : flashCards) {
                boolean isPaper2 = ((StandardFlashCard) flashCard).isPaper2();
                boolean condition;
                switch (contentType) {
                    case StandardFlashCard.PAPER_1:
                        condition = !isPaper2;
                        break;
                    case StandardFlashCard.PAPER_2:
                        condition = isPaper2;
                        break;
                    case StandardFlashCard.ALL:
                        condition = true;
                        break;
                    default:
                        throw new IllegalArgumentException("content type '" + contentType + "' is invalid");
                }
                if (condition) filteredCards.add(flashCard);
            }
            return filteredCards;
        }

        ...
    }

}

A class (object) that implements Topic:

public class CourseTopic implements Topic {

    ...

    public CourseTopic(int id, String identifier, String name, Course course) {
        ...
    }

    @Override
    public int getId() {
        return mId;
    }

    @Override
    public String getIdentifier() {
        return mIdentifier;
    }

    ...


    protected CourseTopic(Parcel in) {
        mId = in.readInt();
        mIdentifier = in.readString();
        mName = in.readString();
        mCourse = in.readParcelable(Course.class.getClassLoader());
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mId);
        dest.writeString(mIdentifier);
        dest.writeString(mName);
        dest.writeParcelable(mCourse, flags);
    }

}

In one of the last lines of the code above, you can see I pass mCourse, which is a Course object I created. Here it is:

public class Course implements Parcelable {

    ...

    public Course(String subject, String examBoard, @FlashCard.CourseType String courseType,
              String revisionGuide) {
        ...
    }


    public String getSubjectIdentifier() {
        return mSubjectIdentifier;
    }

    public String getExamBoardIdentifier() {
        return mBoardIdentifier;
    }

    public ArrayList<Topic> getTopics(Context context) {
        ArrayList<Topic> topics = new ArrayList<>();
        String filename = mSubjectIdentifier + "_" + mBoardIdentifier + "_topics.csv";
        CsvParser parser = CsvUtils.getMyParser();
        try {
            List<String[]> allRows = parser.parseAll(context.getAssets().open(filename));
            for (String[] line : allRows) {
                int id = Integer.parseInt(line[0]);
                topics.add(new CourseTopic(id, line[1], line[2], this));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return topics;
    }

    ...


    protected Course(Parcel in) {
        mSubjectIdentifier = in.readString();
        mBoardIdentifier = in.readString();
        mCourseType = in.readString();
        mRevisionGuide = in.readString();
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mSubjectIdentifier);
        dest.writeString(mBoardIdentifier);
        dest.writeString(mCourseType);
        dest.writeString(mRevisionGuide);
    }

}

I suspect something here may be causing the problem, and is the reason my scenario is different from those in other questions.


To be honest, I'm not exactly sure what may be causing the error, so explanations and guidance in answers would be much appreciated.


Edit:

After David Wasser's suggestions, I have updated parts of my code like so:

FlashCardActivity.java - onCreate(...):

Bundle extras = getIntent().getExtras();
extras.setClassLoader(Topic.class.getClassLoader());
mTopic = extras.getParcelable(EXTRA_TOPIC);

Course.java - writeToParcel(...):

dest.writeString(mSubjectIdentifier);
dest.writeString(mBoardIdentifier);
dest.writeString(mCourseType);
dest.writeInt(mRevisionGuide == null ? 0 : 1);
if (mRevisionGuide != null) dest.writeString(mRevisionGuide);

Course.java - Course(Parcel in):

mSubjectIdentifier = in.readString();
mBoardIdentifier = in.readString();
mCourseType = in.readString();
if (in.readInt() != 0) mRevisionGuide = in.readString();

I've added log messages using Log.d(...) to see if any variables are null when being passed in writeToParcel(...) and used David Wasser's method to properly handle this.

I still get the same error message, however.

解决方案

Your problem is in LanguagesFlashCard. Here are your parcel/unparcel methods:

protected LanguagesFlashCard(Parcel in) {
    mId = in.readInt();
    mEnglish = in.readString();
    mAnswerPrefix = in.readString();
    mAnswer = in.readString();
    mTier = in.readInt();
    mTopic = in.readParcelable(Topic.class.getClassLoader());
}

As you can see, they don't match. The second item you write to the Parcel is an int, the second item you read from the Parcel is a String.

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(mId);
    dest.writeInt(mTier);
    dest.writeString(mEnglish);
    dest.writeString(mAnswerPrefix);
    dest.writeString(mAnswer);
    dest.writeParcelable(mTopic, flags);
}

这篇关于java.lang.RuntimeException:包裹android.os.Parcel:解组未知类型代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆