Check a checkbox from a different thread of the ui. Problem!

Put your problem here if it does not fit any of the other categories.

Check a checkbox from a different thread of the ui. Problem!

Postby anddroidev » Sat Oct 31, 2009 9:52 pm

Hello, i have the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.me.clientandroid;
  3.  
  4.  
  5.  
  6. import android.view.View;
  7.  
  8. import java.io.DataInputStream;
  9.  
  10. import java.io.DataOutputStream;
  11.  
  12. import java.io.IOException;
  13.  
  14. import java.net.Socket;
  15.  
  16. import java.net.UnknownHostException;
  17.  
  18. import android.app.Activity;
  19.  
  20. import android.os.Bundle;
  21.  
  22. import android.view.View.OnClickListener;
  23.  
  24. import android.widget.Button;
  25.  
  26. import android.widget.CheckBox;
  27.  
  28. import java.util.logging.Level;
  29.  
  30. import java.util.logging.Logger;
  31.  
  32.  
  33.  
  34.  
  35.  
  36. public class MainActivity extends Activity implements Runnable {
  37.  
  38.  
  39.  
  40.     Socket socket;
  41.  
  42.     int port = 10000;
  43.  
  44.     String ip = "10.0.2.2";
  45.  
  46.     CheckBox cb1;
  47.  
  48.     Button btn1;
  49.  
  50.  
  51.  
  52.     /** Called when the activity is first created. */
  53.  
  54.     @Override
  55.  
  56.     public void onCreate(Bundle savedInstanceState) {
  57.  
  58.         super.onCreate(savedInstanceState);
  59.  
  60.         setContentView(R.layout.main);
  61.  
  62.  
  63.  
  64.         // Create the widgets
  65.  
  66.         cb1 = (CheckBox) this.findViewById(R.id.widget29);
  67.  
  68.        
  69.  
  70.         cb1.setOnClickListener(new OnClickListener() {
  71.  
  72.  
  73.  
  74.             public void onClick(View arg0) {
  75.  
  76.                 DataOutputStream dos;
  77.  
  78.                 try {
  79.  
  80.                     dos = new DataOutputStream(socket.getOutputStream());
  81.  
  82.                     dos.writeBoolean(cb1.isSelected());
  83.  
  84.                 } catch (IOException ex) {
  85.  
  86.                     Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
  87.  
  88.                 }
  89.  
  90.             }
  91.  
  92.         });
  93.  
  94.  
  95.  
  96.         connectToServer();
  97.  
  98.     }
  99.  
  100.  
  101.  
  102.     public void connectToServer() {
  103.  
  104.         try {
  105.  
  106.             socket = new Socket(ip, port);
  107.  
  108.         } catch (UnknownHostException e) {
  109.  
  110.             e.printStackTrace();
  111.  
  112.         } catch (IOException e) {
  113.  
  114.             e.printStackTrace();
  115.  
  116.         }
  117.  
  118.         Thread thread = new Thread(this);
  119.  
  120.         thread.start();
  121.  
  122.     }
  123.  
  124.  
  125.  
  126.     public void run() {
  127.  
  128.         DataInputStream dis = null;
  129.  
  130.         boolean flag;
  131.  
  132.         try {
  133.  
  134.             dis = new DataInputStream(socket.getInputStream());
  135.  
  136.         } catch (IOException e1) {
  137.  
  138.             e1.printStackTrace();
  139.  
  140.         }
  141.  
  142.         while (true) {
  143.  
  144.             try {
  145.  
  146.                 flag = dis.readBoolean();
  147.  
  148.                 <span style="color: red">cb1.setChecked(flag);</span>
  149.  
  150.             } catch (IOException e) {
  151.  
  152.                 e.printStackTrace();
  153.  
  154.             }
  155.  
  156.         }
  157.  
  158.     }
  159.  
  160. }
  161.  
  162.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


When i run the program i get the following error: "android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views"

the problem is on this line: "cb1.setChecked(flag);" when i erase the line everything works perfect, so i think that the problem occurs because i trying to check the checkbox from a different thread of the ui . I have wrote the same program in java desktop and it is working. Can anyone explane me the problem and gine me a solution on that? Thank you.
User avatar
anddroidev
Freshman
Freshman
 
Posts: 2
Joined: Sat Oct 31, 2009 6:27 pm

Top

Postby padde » Sun Nov 01, 2009 12:44 am

In Android only the UI thread can manipulate UI elements.
You could use a handler send a message from the worker thread
to the UI thread to update the UI element or use the new (since 1.5 i think)
AsynTask class.. its build for exactly those situations.
For code examples try the search function or read the android developer site :)
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby anddroidev » Sun Nov 01, 2009 12:53 am

Thanks for your help, the problem solved.
User avatar
anddroidev
Freshman
Freshman
 
Posts: 2
Joined: Sat Oct 31, 2009 6:27 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests