你能在一个线程中设置textview文本吗?

时间:2012-04-29 17:57:50

标签: android multithreading textview logcat

一旦应用程序停止从服务器恢复文本,说它现在已被删除,我需要它才能拥有它我需要设置按钮说它已断开连接,这就是我现在所拥有的(这是一个while循环我只是没有发布所有代码)

Thread.sleep(10);
                        counter++;
                        if (counter >= 100)
                        {
                            Log.d("ClientActivity","send S");
                            counter = 0;
                            out.println("S");
                            A2MCString = in.readLine();
                            Log.d("ClientActivity","got " + A2MCString);
                            if (A2MCString == null) 
                                {
                                connected = false;
                                Log.d("ClientActivity","Closed1");
                                Connect.setText("Connect");
                                Log.d("ClientActivity","Closed2");
                                Connect.setBackgroundResource(R.drawable.contect_button);
                                Log.d("ClientActivity","Closed3");
                                DeBug.setText("Disconnected from " + serverIpAddress);
                                Log.d("ClientActivity","Closed4");
                                }

当我尝试设置连接到“连接”时,应用程序崩溃logCat错误是“android.view.ViewRoot $ CalledFromWrongThreadException”如果你可以帮助我那就太棒了!

1 个答案:

答案 0 :(得分:1)

使用runOnUiThread或处理程序从线程更改UI ..我认为这一行导致错误..

Connect.setBackgroundResource(R.drawable.contect_button);

将它放在你的线程中的runOnUiThread中,就像这样

       runOnUiThread(new Runnable() {
                     public void run() {

                        Connect.setBackgroundResource(R.drawable.contect_button);
                    }
                });