我似乎在我的应用程序中遇到一个奇怪的错误(参见GitHub),当我将对象传递给实现Parcelable的不同活动时会发生这种错误.

我已经在Stack Overflow上检查了其他问题和答案,但我无法找到解决方案.我已经尝试了答案here,例如 – 这里它是供参考:

-keepclassmembers class * implements android.os.Parcelable {

static ** CREATOR;

}

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

此外,我问一个新问题的原因是因为我认为我的问题是由于我在我的应用程序中使用接口的原因(稍后我将在这一点上进行扩展). Stack Overflow上的其他问题不适合我的特定情况.

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

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)

...

我将上述活动称为(也是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);

要考虑的主要部分是我通过mTopic.这是我创建的Topic interface.

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

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

public interface Topic extends Parcelable {

int getId();

String getIdentifier();

String getName();

Course getCourse();

ArrayList getFlashCards(Context context);

class FlashCardsRetriever {

public static ArrayList filterStandardCards(ArrayList flashCards, @StandardFlashCard.ContentType int contentType) {

ArrayList 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;

}

...

}

}

实现主题的类(对象):

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 CREATOR = new Parcelable.Creator() {

@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 getTopics(Context context) {

ArrayList topics = new ArrayList<>();

String filename = mSubjectIdentifier + "_" + mBoardIdentifier + "_topics.csv";

CsvParser parser = CsvUtils.getMyParser();

try {

List 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 CREATOR = new Creator() {

@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 – 课程(包裹):

mSubjectIdentifier = in.readString();

mBoardIdentifier = in.readString();

mCourseType = in.readString();

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

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

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

android os parcel,java.lang.RuntimeException:Parcel android.os.Parcel:...相关推荐

  1. Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPoi

    Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPoi ...

  2. Android之提示java.lang.RuntimeException: Parcel: unable to marshal value Image问题

    1 问题 使用Intent携带数据(putExtra)跳转activity,提示如下错误 04-18 22:42:49.664 16194 16194 E AndroidRuntime: Proces ...

  3. Android 自定义View java.lang.RuntimeException: Unable to start activity ComponentInfo

    在Android 4.1.2环境下,自定义了一个View, 运行时出现 java.lang.RuntimeException: Unable to start activity ComponentIn ...

  4. Android 编程下 java.lang.NoClassDefFoundError: cn.jpush.android.api.JPushInterface 报错

    使用了极光推送的 jar 包项目在从 SVN 中检出后,如果不重新对 jar 包和 Bulid Path 进行配置就会抛出 java.lang.NoClassDefFoundError: cn.jpu ...

  5. java.lang.RuntimeException: setDataSource failed: status = 0x80000000

    获取视频的时长的方法: try {MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataS ...

  6. java 线程 handler,java.lang.RuntimeException:处理程序(android.os.Handler)在死线程上向处理程序发送消息...

    在我的应用程序中,我使用IntentService发送短信. @Override protected void onHandleIntent(Intent intent) { Bundle data ...

  7. java.lang.RuntimeException: Parcel: unable to marshal value com.

    今天出现这个错百度一下才知道我的javabean没有实现序列化导致的错误 java.lang.RuntimeException: Parcel: unable to marshal value错误引起 ...

  8. Java.lang.RuntimeException: Parcel: unable to marshal value

    开发中遇到的问题 使用Parcelable 序列化了一个数组对象.在kotlin中实现Parcelable 序列化,在construct内写入 自己对应的List的Bean. ArrayList< ...

  9. Android :java.lang.RuntimeException: takePicture failed

    错误堆栈: --------- beginning of crash 2020-09-14 13:43:51.723 10343-10343/com.xiaomi.micolauncher E/And ...

  10. 解决 Android java.lang.RuntimeException: Stub!

    错误堆栈: java.lang.RuntimeException: Stub!at org.apache.http.message.AbstractHttpMessage.<init>(S ...

最新文章

  1. php 分类标签推荐,MySQL / PHP:通过标签/分类法查找类似/相关的项目
  2. Spring的datasource配置详解
  3. 【WebView】warnning:所有WebView方法必须在主线程调用(4.0) 所有WebView方法必须在同一线程调用(4.4)
  4. 网众linux安装教程,网众Linux搭建Samba教程
  5. 常用的lucene分词器-笔记
  6. 从代码层面优化系统性能的解决方案
  7. Play on Words UVA - 10129 (欧拉回路)
  8. JAVA加密解密→术语、密码分类、OSI与TCP/IP安全体系、Base64、消息摘要算法MD/SHA/MAC、对称加密算法DES/AES/PBE、非对称加密算法DH/RSA/EIGamaI
  9. 在access中一列称为_ACCESS考试_笔试
  10. 合作的进化 6-10
  11. linux环境下使用logrotate工具实现nginx日志切割
  12. ArchiSteamFarm(ASF优秀的Steam挂卡工具) V4.0.3.3绿色版
  13. excel切片器_干货分享:Excel数据透视表操作技巧,帮你提升工作效率
  14. arduino烧录_arduino 烧录 attiny85
  15. 设置vscode默认终端为msys/MinGW32/MinGW64
  16. vue 超出三行隐藏_文字超出三行省略...显示全文
  17. es 启动elasticsearch.bat发生闪退
  18. VMware12安装图解
  19. 计算机右键管理无法访问指定设备,简单几步解决win10打开任务管理器显示无法访问指定设备方法...
  20. 如何判断一个字符串里有多少个汉字?(原理及过程)

热门文章

  1. 文献阅读——金属伪影减少MAR问题
  2. Permission denied: user=10273, access=WRITE, inode=“/cou/jd_phone_list“:root:supergroup:-rw-r--r--
  3. 在linux中访问权限是755,在Linux系统中,一个文件的访问权限是755,其含义是什么?...
  4. Steam版XCOM: Enemy Within(内部敌人)不能启动的问题
  5. 数据库expecting ''', found 'EOF'异常——原载于我的百度空间
  6. Codeblock 美化字体和主题
  7. 想从事Python后端开发?如何入门和学习,这篇文章来告诉你。
  8. 原生 js html 开发成桌面应用 以及打包
  9. 微信跳转浏览器html5,微信跳转浏览器或提示手机端打开HTML代码 最新
  10. EasyOCR,识别图片中的文字真的so easy