0

I created an application which uses a socket connection to my server. This server sends an information string to the phone which reads this with a BufferedReader. Now i want to display this information on the screen(in a textbox for example) but the textbox.append command doesn't work in this case. There is no error but at runtime it won't add the string to the textbox. Tried the same with textviews. Here the part of this code. The commands() function is called in the connect progress and the variables are declared at the beginning.

public void commands() throws Exception{

Responce = buffer.readLine();
final TextView textViewToChange = (TextView) findViewById(R.id.textView1);
textViewToChange.setText(Responce);

commands(); }

Would be nice if anyone know how to fix this problem. Thanks :)

user2295158
  • 72
  • 1
  • 1
  • 7
  • read your stacktrace. the exception message is very clear as to what is happening. – njzk2 Apr 18 '13 at 13:18
  • sry but there was no exception... it just didn't work properly – user2295158 Apr 18 '13 at 18:54
  • are you sure ? you are throwing an Exception in your commands method. do you surround your call to commands by a try catch block ? if so, what do you do in the catch block ? – njzk2 Apr 19 '13 at 07:47
  • my method changed the text only one time, after this the app lost the connection to the server or freezed. But anyway the accepted method works great now;) – user2295158 Apr 19 '13 at 12:47

2 Answers2

1

UI in android is not updateble from another Thread. Look at

 AsyncTask

Read this also Ui Update Android

Community
  • 1
  • 1
Eugene
  • 117,005
  • 15
  • 201
  • 306
1

You need to update UI on the main UI Thread. You can use runOnUiThread as below.

  runOnUiThread(new Runnable() //run on ui thread
                 {
                  public void run() 
                  {       
                      textViewToChange.setText(Responce);
                 }
                 });
Raghunandan
  • 132,755
  • 26
  • 225
  • 256