你可以设置一个线程中的TextView的文本? [英] Can you set textview text within a thread?

查看:126
本文介绍了你可以设置一个线程中的TextView的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要什么一旦应用程序停止reciving从服务器上说,它现在disconected我需要设置按钮的说,它是断开的文字就拥有它,这就是我现在所拥有的(这是一个while循环我只是剪掉后所有code的)

What I need it to have it once the app stops reciving text from the server saying that it is now disconected I need to set the button to say that it is disconnected, this is what I have right now (this is in a while loop I just didnt post all of the code)

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如果你能帮助我,这将是伟大的!

the app crashes when I try to set connect to "Connect" the logCat error is "android.view.ViewRoot$CalledFromWrongThreadException" If you could help me it would be great!

推荐答案

使用runOnUiThread或处理程序进行更改从一个线程到UI ..我认为此行是导致错误。

Use runOnUiThread or a handler to make changes to UI from a thread.. I think this line is causing the error..

Connect.setBackgroundResource(R.drawable.contect_button);

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

put it in runOnUiThread in your thread like this

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

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

这篇关于你可以设置一个线程中的TextView的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆