将一个对象放入Handler消息中
我需要从互联网下载图像, 在不同的线程中,
然后将处理程序消息中的图像对象发送到 UI 线程。
我已经有了这个:
...
Message msg = Message.obtain();
Bundle b = new Bundle();
b.putParcelable("MyObject", (Parcelable) object);
msg.setData(b);
handler.sendMessage(msg);
当我收到这条消息时, 我想提取该对象:
...
public void handleMessage(Message msg) {
super.handleMessage(msg);
MyObject objectRcvd = (MyObject) msg.getData().getParcelable("IpTile");
addToCache(ipTile);
mapView.invalidate();
}
但这给了我:
...java.lang.ClassCastException...
有人可以帮忙吗?
顺便问一下,这是最有效的方法吗
将对象传递给 UI 线程?
谢谢大家!
I need to download an image from the internet,
in a different thread,
and then send that image object in the handler message, to the UI thread.
I already have this:
...
Message msg = Message.obtain();
Bundle b = new Bundle();
b.putParcelable("MyObject", (Parcelable) object);
msg.setData(b);
handler.sendMessage(msg);
And when I receive this message,
I want to extract the object:
...
public void handleMessage(Message msg) {
super.handleMessage(msg);
MyObject objectRcvd = (MyObject) msg.getData().getParcelable("IpTile");
addToCache(ipTile);
mapView.invalidate();
}
But this is giving me:
...java.lang.ClassCastException...
Can anyone help?
And by the way, is this the most efficient way
to pass an object to the UI Thread?
Thank you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道我迟到了,但如果您在单个进程中使用该服务,则有一种更简单的方法。您可以使用此行将任意
对象
附加到您的消息
:这对我当前的项目来说效果很好。
哦,我正在放弃使用
AsyncTask
对象,因为我相信它们过多地增加了代码耦合。I know I'm late to the party, but there is an easier way if you are using the service within a single process. You can attach any arbitrary
Object
to yourMessage
using this line:This is working well for me in my current projects.
Oh, and I'm moving away from using
AsyncTask
objects, as I believe they increase code coupling too much.第一:你到底从哪里得到异常?将实例放入捆绑包时,还是检索实例时?
我相信你把事情搞混了。创建捆绑包时,您可以编写
“因此您将实例“
objet
””分配给键“MyObject
”。但是,在检索实例时,您可以这样写:这里,您正在从键“
IpTile
”检索实例。请注意“IpTile”!=“MyObject”
。尝试使用以下内容来检索对象:或者相反,尝试用以下代码替换将实例放入捆绑包中的代码:
另外要检查的几点:
MyObject
是否实现可打包
? (我想是的,否则你将无法编译)object
是否包含实现Parcelable
的实例?First: Where exactly do you get the exception? When putting the instance into the bundle, or upon retrieving it?
I believe your mixing things up. When creating your bundle, you write
So you are assigning the instance "
objet
" to the key "MyObject
". But when retrieving your instance you write:Here, you are retrieving an instance from the key "
IpTile
". Note that"IpTile" != "MyObject"
. Try using the following to retrieve the object:or the other way around, try replacing your code which puts the instance into the bundle with this:
Another few points to check:
MyObject
implementParcelable
? (I suppose so, otherwise you wouldn't be able to compile)object
contain an instance which implementsParcelable
?我会使用 AsyncTask 来执行此类操作。它允许您连接到您的 ui 线程以进行进度更新和完成下载等操作。下面的例子展示了应该如何做:
希望有帮助
I would use an AsyncTask for this kind of operation. It allows you to hook into your ui thread for things like progress updates and once you have finished your download. The example below shows how one should be done:
Hope that helps
如果您只转发一个对象,则可以使用 Message 类来完成,无需创建捆绑包 - 它更容易、更快。
经典地检索它
享受:)
If you are forwarding only one object, you can do it using the Message class, you don't need to create a bundle - it's easier and faster.
Retrieve it classicaly
Enjoy :)