Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Hi and thank you for looking at my question. I am an intermediate programmer in C but an Android newbie. I have been trying to get a chat programming working. Assuming everything else in the code below works perfectly. The one question I like to ask is when I try to setText() from a thread running, I get an exception above. I looked at many many websites and here too. Found many things, but I really do not understand. Please explain to me in the most simple way or offer me some simple fix if possible.

Thank you very much!!

public class chatter extends Activity {

private String name = "Unknown User";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    final EditText msgToServer = (EditText) findViewById(R.id.msgBox);
    final EditText chatFromServer = (EditText) findViewById(R.id.chatBox); 

    final Button MsgToServer = (Button) findViewById(R.id.sendButton);

    Socket socket = null;
    String ipAddress = "192.168.1.103";
    try {
        InetAddress serverAddr = InetAddress.getByName(ipAddress);
        Socket socketMain = new Socket(serverAddr, 4444);
        socket = socketMain;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("TCP", "error", e);
    }

    final OutMsg outMsg = new OutMsg(socket);
    Thread msgSenderThread = new Thread(outMsg);
    msgSenderThread.start();

    //chatFromServer.post(new InMsg(socket, chatFromServer));
    Thread msgReceiverThread = new Thread(new InMsg(socket, chatFromServer));
    msgReceiverThread.start();

    MsgToServer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String msgToServerString; 
            msgToServerString = msgToServer.getText().toString();
            outMsg.message = name + ": " + msgToServerString;
            outMsg.readyToSend = true;
            msgToServer.setText("");
        }
    });
}

public void updateResultsInUi (String msg)
{
    final EditText chatFromServer = (EditText) findViewById(R.id.chatBox); 
    chatFromServer.setText(msg); 
}

public class InMsg implements Runnable {

    Socket socket;
    EditText chatFromServer;
    public InMsg(Socket socket, EditText chatFromServer)
    {
        this.socket = socket;
        this.chatFromServer = chatFromServer;
    }

    public void run(){
        try {
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str = "FIRSTMESSAGEFROMSERVER";
            while (true)
            {
                if (str.equals("FIRSTMESSAGEFROMSERVER"))
                    str = in.readLine();
                else
                    str = str + "\n" + in.readLine();
                Log.e("TCP", "got the message: " + str);
     //Here is where went wrong******************
                chatFromServer.setText(str);
     //******************************************
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("TCP", "error in receiving", e);
        }
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.setNameMenu:
        setname();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void populateChatBox (String msgFromS)
{
    Log.e("TCP", "going in to popC");
    final EditText textNameInput = (EditText) findViewById(R.id.nameBox);
    Log.e("TCP", " popC");
    textNameInput.setText(msgFromS);
    Log.e("TCP", "going out from popC");
}

public void setname()
{
    setContentView(R.layout.custom_dialog);
    final EditText textNameInput = (EditText) findViewById(R.id.nameBox);
    Button submitNameButton = (Button) findViewById(R.id.submitNameButton);
    submitNameButton.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
        String nameinput = textNameInput.getText().toString();
            if (!name.equals(""))
                name = nameinput;
            setContentView(R.layout.main);
        }
    });
}
}
like image 998
Byte Avatar asked Feb 23 '11 21:02

Byte


People also ask

How do you fix this only the original thread that created a view hierarchy can touch its views?

To fix this error, wrap the code that has to be executed on UI thread in a Runnable instance passed to runOnUiThread() method.

How does runOnUiThread work?

Basically what runOnUiThread() will do is - Runs the specified action on the UI thread. It will check the current thread and if it finds its the MainThread it will execute that task immediately , otherwise first it will switch you to app MainThread and then it will execute the given task.

What is a handler in Android Studio?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper .


1 Answers

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
        }
};
like image 82
Veeresh Avatar answered Oct 21 '22 08:10

Veeresh