使用android捆绑包时解组未知类型代码

使用android捆绑包时解组未知类型代码

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

问题描述

我收到以下错误消息:

java.lang.RuntimeException: Parcel android.os.Parcel@41141190: Unmarshalling unknown type code 7602286 at offset 16
at android.os.Parcel.readValue(Parcel.java:1921)
at android.os.Parcel.readMapInternal(Parcel.java:2094)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getFloat(Bundle.java:981)

我正在使用WiFi Direct将对象作为消息发送。因此,我在发送时将对象转换为字节数组,而在接收时将其反转。

I am sending an object as a message using WiFi direct. Hence I am converting the object into byte array while sending and reversing the conversion while receiving.

我的对象有两个字段-一个String和一个android bundle。发送时,我正在填充字符串字段,并使用键将float值放入android bundle中。

My object has two fields - one String and one android bundle. While sending I am populating the string field and putting a float value in the android bundle using a key.

我能够在接收方的末端检索字符串值。当我尝试使用 getFloat 方法检索包中存在的浮点值时,就会出现错误。

I am able to retrieve the string value at the receiver's end. The error comes when I try to retrieve the float value present inside the bundle using getFloat method. What could be the reason for this?

推荐答案

花了很多时间后,我找到了解决方法,这是我在Parcelable类中犯了错误,我忘了编写和读取参数之一,最后我解决了问题,我的代码如下:

After spending lots of hour i found solution, that i made mistake in Parcelable class, i forgot to write and read one of the parameters, finally i solved issue, my code look like:

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(filepath);
    dest.writeString(filename);
    dest.writeString(fileCount);
    dest.writeInt(index);
}

public YourParcelableClassName(Parcel in){
    filepath = in.readString();
    filename = in.readString();
    fileCount = in.readString();
    index = in.readInt();
}

第一次活动通过数据中,

in the first activity pass data,

Intent mIntent = new Intent(YourFirstActivity.this,YourSecondActivity.class);
mIntent.putExtra("position",position);
mIntent.putParcelableArrayListExtra("filedata",parcelableArrayList);
startActivity(mIntent);

用于将意图数据纳入第二项活动,

for getting intent data into second activity,

int Position = getIntent().getIntExtra("position",0);
parcelableArrayList = getIntent().getParcelableArrayListExtra("filedata");

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

08-01 07:48