You can only access UI objects from the main (UI) thread.

There are several ways to "post" UI operations to the UI thread.

This one is probably the easiest: Activity.runOnUIThread(Runnable action)

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

-- Kostya

16.10.2010 20:58, chcat пишет:
I have an Activity with TextView that I am trying to update from
different thread. To do that i pass TextView to the "updater" thread
onCreate(..)
..
txtStatus = (TextView)this.findViewById(R.id.status);
// start udp server as separate thread and pass TextView to that
thread to print text messages from udp socket

new Thread(new TelemetryServer(txtStatus)).start();
//see thread code below


As a result i have thread error, even though "updater" is
synchronized:
ERROR/UDP(282): android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its
views.
Any other ideas how to solve this problem?
Tnank you,
-V


public class TelemetryServer implements Runnable {
        public static final String SERVERIP = "127.0.0.1"; // 'Within' the
emulator!
        public static final int SERVERPORT = 2222;
        private TextView m_view;
        private DatagramSocket socket;
        private boolean loop;
        private synchronized void update( String msg)
        {
                m_view.append("\n");
                m_view.append(msg);
        }

        public TelemetryServer(TextView _view)
        {
                m_view = _view;
                loop = true;
        }
        public void run() {
                try {
                                InetAddress serverAddr = 
InetAddress.getByName(SERVERIP);
                                Log.d("UDP", "S: Connecting...");

                                socket = new DatagramSocket(SERVERPORT,
                                                serverAddr);
                                Log.i("TelemetryServer", "started");
                                while(loop) {
                                        byte[] buf = new byte[1024];
                                        DatagramPacket packet = new 
DatagramPacket(buf, buf.length);
                                        socket.receive(packet);
                                        update(new String(packet.getData()));
                                }

                        } catch (Exception e) {

                                Log.e("UDP", "S: Error", e);

                        } // end of try
                        Log.d("UDP", "S: Done.");
                        socket.close();
        }
}




--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
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

Reply via email to