0

一旦应用程序停止从服务器接收文本说它现在已断开连接,我需要它来拥有它我需要设置按钮以表示它已断开连接,这就是我现在所拥有的(这是在一个 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”如果你能帮助我,那就太好了!

4

1 回答 1

1

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

Connect.setBackgroundResource(R.drawable.contect_button);

像这样把它放在你的线程中的 runOnUiThread

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

                        Connect.setBackgroundResource(R.drawable.contect_button);
                    }
                });
于 2012-04-29T18:01:05.903 回答