error: Only the original thread that created a view hierarchy can touch its views error: Only the original thread that created a view hierarchy can touch its views android android

error: Only the original thread that created a view hierarchy can touch its views


In your run() method:

Message msg = new Message();String textTochange = "text";msg.obj = textTochange;mHandler.sendMessage(msg);

Create the mHandler in your UI thread;

Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            String text = (String)msg.obj;            //call setText here        }};


You are not on the UI thread when setting the text. You need to be on UI if you want to work on UI items. Create a message handler on the UI thread, post your messages to it and call setText from the handler on the UI thread.


you can do this whenever you are in a thread:

msgToServer.post(new Runnable() {    public void run() {        msgToServer.setText("your text here");    }}