Try changing this: _msgHandler.dispatchMessage(msg); to _msgHandler.sendMessage(msg);
Then read the docs on the Handler class. On Mon, Apr 18, 2011 at 11:24 AM, BrunoLemos <[email protected]> wrote: > Hello everybody, > > I'm having a little problem with using threads and handlers. > > I have a running activity in the main thread, where I create a worker > thread to connect to my server through sockets. To treat the returns I > am using a Handler created in main thread, and these messages should > fall back into main thread, but still in worker thread. > > Could anyone help me? > > Sorry for english, I'm using google translate. > > > > x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x > Error message: > > Thread [<15> Thread-9] (Suspended (exception ViewRoot > $CalledFromWrongThreadException)) > ViewRoot.checkThread() line: 2683 > ViewRoot.invalidateChild(View, Rect) line: 570 > ViewRoot.invalidateChildInParent(int[], Rect) line: 596 > LinearLayout(ViewGroup).invalidateChild(View, Rect) line: 2396 > TextView(View).invalidate() line: 4945 > TextView.updateAfterEdit() line: 4736 > TextView.handleTextChanged(CharSequence, int, int, int) line: 6158 > TextView$ChangeWatcher.onTextChanged(CharSequence, int, int, int) > line: 6315 > SpannableStringBuilder.sendTextChange(TextWatcher[], int, int, int) > line: 889 > SpannableStringBuilder.change(boolean, int, int, CharSequence, int, > int) line: 352 > SpannableStringBuilder.change(int, int, CharSequence, int, int) > line: > 269 > SpannableStringBuilder.replace(int, int, CharSequence, int, int) > line: 432 > SpannableStringBuilder.append(CharSequence, int, int) line: 259 > SpannableStringBuilder.append(CharSequence, int, int) line: 28 > SpannableStringBuilder(TextView).append(CharSequence, int, int) > line: > 2236 > TextView.append(CharSequence) line: 2223 > tela.logMessage(String) line: 41 > String(tela$1).handleMessage(Message) line: 16 > tela$1(Handler).dispatchMessage(Message) line: 99 > CmaConnection.run() line: 103 > Thread.run() line: 1096 > > > > x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x > main thread: > > public class tela extends Activity { > > private Handler messageHandler = new Handler() { > public void handleMessage(Message msg) { > logMessage((String)msg.obj); > } > }; > > public TextView txtIn; > public Button btConn; > > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > txtIn = (TextView)findViewById(R.id.txres); > btConn = (Button)findViewById(R.id.btcon); > } > > public void doConnect(View view){ > this.txtIn.append("Start thread..."); > > CmaConnection __conn = new CmaConnection("127.0.0.1", 9090, > messageHandler); > new Thread(__conn).start(); > } > > public void logMessage(String msg){ > > ////////////////////// > // Bug is here // > ////////////////////// > this.txtIn.append(msg); > } > } > > > x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x > worker thread: > > public class CmaConnection extends Thread{ > > private Socket _conn; > private DataInputStream dIn = null; > > private String _addr; > private Integer _port; > private Handler _msgHandler; > > public CmaConnection(String address, Integer port, Handler > messageHandler) { > _addr = address; > _port = port; > _msgHandler = messageHandler; > } > > public void run() { > > try { > _conn = new Socket(_addr, _port, true); > > dIn = new DataInputStream(_conn.getInputStream()); > > Message msg = new Message(); > > while(_conn.isConnected()){ > msg.obj = dIn.readLine(); > _msgHandler.dispatchMessage(msg); > } > > _conn.close(); > > } catch (UnknownHostException e) { > e.printStackTrace(); > } catch (IOException e) { > e.printStackTrace(); > } > } > } > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- ~ Jeremiah:9:23-24 Android 2D MMORPG: http://solrpg.com/, http://www.youtube.com/user/revoltingx -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

