我会尽量保持简单:
在我的主要活动中,我创建了一个处理程序:
public class ARViewer extends ARDisplayActivity {
public final MHandler mHandler = new MHandler(this);
public void onCreate(Bundle savedInstanceState) {
...
mhandler类:
public final class MHandler extends Handler{
//main activity
private ARViewer arnv;
public MHandler(ARViewer arnv){
this.arnv = arnv;
}
@Override
public void handleMessage(Message msg) {
...
case H_RR :
arnv.setContentView(R.layout.routeplanner);
break;
...
super.handleMessage(msg);
}
}
但如果我从另一个类的回调函数调用handleMessage方法,肯定是从另一个线程调用,我仍然会收到异常消息:
CalledFromWrongThreadException (Only the original thread that created a view hierarchy can touch its views)
:public void rFound(Route route) {
Message msg = new Message();
msg.what = MHandler.H_RR;
ARViewer.arnv.mHandler.handleMessage(msg);
}
最佳答案
你不需要提及那里的活动。
创建一个新的runnable,你可以在那里做你的ui工作。做mhandler.post(myuirunnable);
示例如下:
http://developer.android.com/guide/appendix/faq/commontasks.html#threading