解组 Parcelable 时出现 ClassNotFoundException
我在解析 Intent 上的 Parcelable 时遇到问题。
来创建 Parcelable
Intent data = new Intent();
data.putExtra(ShoppingListAdapter.parcelName, la);
setResult(Activity.RESULT_OK, data);
我使用在 onActivityResult 中接收它
Parcelable myData = data.getParcelableExtra(ShoppingListAdapter.parcelName);
:然后使用它将其传递给另一个 Activity:
Intent myIntent = new Intent(this,Class.class);
myIntent.putExtra("myData", myData);
startActivityForResult(myIntent, RESULT);
我的 Parcelable 有另一个 Parcelable,我在其中写入和读取 uisng:
list = in.readParcelable(null);
我尝试使用不同的类加载器,从 ClassLoader.getSystemLoader() 到 MyClass.class .getClassLoader() 但我仍然收到运行时异常:
06-12 21:13:04.940: ERROR/AndroidRuntime(29962): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
我的包裹在此之前的某个地方是否已损坏,或者我读错了?
亚历克斯
I have a problem with parsing a Parcelable across an Intent.
I create parcelable using
Intent data = new Intent();
data.putExtra(ShoppingListAdapter.parcelName, la);
setResult(Activity.RESULT_OK, data);
I receive it in the onActivityResult:
Parcelable myData = data.getParcelableExtra(ShoppingListAdapter.parcelName);
Then pass it to another Activity using:
Intent myIntent = new Intent(this,Class.class);
myIntent.putExtra("myData", myData);
startActivityForResult(myIntent, RESULT);
My Parcelable has another Parcelable inside which I write and read uisng:
list = in.readParcelable(null);
I have tried using different class loaders, from ClassLoader.getSystemLoader() to MyClass.class.getClassLoader() but still I get a Runtime Exception:
06-12 21:13:04.940: ERROR/AndroidRuntime(29962): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
Is my Parcel corrupted somewhere before this or am I reading it wrong?
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到了这个问题,这是一个愚蠢的事情。 writeToParcel 和 readToParcel 方法的顺序和数量应该相同并且具有相同的变量。
我的意思是:
必须与以下顺序相同:
I had that problem too, it's a silly thing. The order and number of the methods on writeToParcel and readToParcel should be the same and with the same variables.
I mean:
must be in the same order as:
正如所指出的,这是一种不正确的使用方法。所以虽然我还没有解决这个问题,但有更好的方法来做同样的事情。因此,请考虑一下
如何在Android中声明全局变量?
尽管如果有人有对原始问题的答案有任何想法请发布。可能发生在其他地方的其他人身上。
亚历克斯
As was pointed out this was an incorrect approach to use. So although I haven't solved it, there is a better way of doing the same thing. So consider it instead
How to declare global variables in Android?
Although if anyone has any idea for the answer to the original question please post. Can happen somewhere else to someone else.
Alex
是否 Intent.setExtrasClassLoader(java.lang.类加载器) 有帮助吗?
Does Intent.setExtrasClassLoader(java.lang.ClassLoader) help?