views:

113

answers:

2

I will try to keep it simple:

In my main activity I make a handler:

public class ARViewer extends ARDisplayActivity {

    public final MHandler mHandler = new MHandler(this);

 public void onCreate(Bundle savedInstanceState) {
...

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

But if I call the handleMessage method from a callback function in a other Class, definitely from a other thread, I still get the exception 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  A: 

You don't need reference to activity there. Create new runnable where you doing your UI stuff. And do mHandler.post(myUIRunnable); Example is here: http://developer.android.com/guide/appendix/faq/commontasks.html#threading

Alex Volovoy
i found this solution before but can't get it to work. I will look again to it. thanks
michel
Why you're setting setContentView(R.layout.routeplanner) in the thread ? Set it in onCreate and then update your views / layouts by findViewById(R.id.myview) and setting right content/values/visibility
Alex Volovoy
there are a lot more views(groups) than only 'routeplanner', so i want a handler for all views
michel
works perfect now. thx
michel
A: 

You should call sendMessage() for Handler with the message id as H_RR. This will automatically call the handleMessage() in main thread.

Karan