Cannot resolve CalledFromWrongThreadException using handler

I will try to make it simple:

In my main activity, I do a handler:

public class ARViewer extends ARDisplayActivity { public final MHandler mHandler = new MHandler(this); public void onCreate(Bundle savedInstanceState) { ... 

MHandler Class:

 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); } } 

But if I call the handleMessage method from a callback function in another class, definitely from a different thread, I still get the error message: 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); } 
+2
source share
2 answers

You do not need a link to activity. Create a new runnable where you make your user interface. And do mHandler.post (myUIRunnable); Example here: http://developer.android.com/guide/appendix/faq/commontasks.html#threading

+3
source

You must call sendMessage () on the handler with the message identifier as H_RR. This will automatically call handleMessage () in the main thread.

0
source

All Articles