为什么getHandler()返回null? [英] Why getHandler() returns null?
问题描述
我有以下问题。我溺水的MapView的路线在不同的线程是这样的:
I have got following problem. I'm drawning route on mapview in separate thread like this:
public void drawRoute(final MapView mapView) {
new Thread(new Runnable() {
public void run() {
try {
//Do something useful
} catch (SomeException se) {
Handler handler = mapView.getHandler();
handler.post(/*show error in UI thread*/)
}}
}).start();
}
但是,当我得到处理它返回null,虽然在调试模式下处理器返回并显示错误消息。可问题是什么?
But when I get handler it returns null, although in debug mode handler returned and error message is displayed. What can the problem be?
PS可能是不正确的方式得到处理程序,但我找不到有关它的信息。
PS May be it's incorrect way to get Handler, but I couldn't find information about it.
推荐答案
的 getHandler
方法的返回值空
,因为观点并没有附:
The getHandler
method returns null
because the view is not attached:
public Handler getHandler() {
if (mAttachInfo != null) {
return mAttachInfo.mHandler;
}
return null;
}
mAttachInfo
在 dispatchAttachedToWindow
设置和调零的 dispatchDetachedFromWindow
。
而不是 mapView.getHandler()的。后()
您可以直接使用的 mapView.post()
(这似乎是使用 getHandler()后()
或 ViewRootImpl.getRunQueue()后()
)。
Instead of mapView.getHandler().post()
you can use directly mapView.post()
(which seems to use getHandler().post()
or ViewRootImpl.getRunQueue().post()
).
这篇关于为什么getHandler()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!