AhmadAssaf AhmadAssaf - 1 year ago 69
Android Question

Updating Android UI using threads

I am writing an android chat app, i am listening for connections and i receive data and i can see it in the Log.d, but whenever i try to update my UI the application crash .. the code is

private class chatReceiver implements Runnable {
public void run() {
try {
skt = new DatagramSocket(Integer.parseInt(Main.prefs.getString("port_number", "5432")));
DatagramPacket rcvPkt = new DatagramPacket(rcvBuf,rcvBuf.length);
String ack = "Hello from our SimpleUDPServer";
byte[] sndBuf = ack.getBytes();
while (true) {
Log.d("Server received: " ,"entered loop");
String rcvMsg = new String(rcvBuf, 0, rcvPkt.getLength(), "UTF-8");
Log.d("Server received: " ,"receiving" + rcvMsg);
if (rcvMsg != null) {
Log.d("Server received: " ,"not equal null");
// I want to update my UI here
DatagramPacket k = new DatagramPacket(sndBuf, sndBuf.length,
rcvPkt.getAddress(), rcvPkt.getPort());
Log.d("Server sent" ,ack);

} catch (IOException ex) {
Log.d("ThreadStart", "Error Starting thread" + ex.getStackTrace());


and to update the UI i use :

public static void updateUI(Bubble b, View itemView) {

TextView txt_display_name = (TextView) itemView
TextView txt_chat_body = (TextView) itemView
TextView txt_creation_date = (TextView) itemView

my app keeps on crashing !!

Answer Source

you cannot touch anything in the ui thread from a background thread, to do that use Handlers, initialize your background thread passing it a Handler object. When data arrives use the handler to send a message to the ui. In the ui when the message from the background thread comes, just update the Views.

something like this...

in the background thread:

    Message msg = handler.obtainMessage();
    msg.what = UPDATE_IMAGE;
    msg.obj = bitmap;
    msg.arg1 = index;

in the ui thread:

final Handler handler = new Handler(){
  public void handleMessage(Message msg) {
      images.get(msg.arg1).setImageBitmap((Bitmap) msg.obj);

and pass the handler to the bakground thead