title: buttonConnectOnClickListener, FATAL EXCEPTION: AsyncTask #1
body: <p>The Android client runs with no problems on Android 2.2. But in Android 4.2 i need to use AsyncTask, and i dont know how to properly set it up. On button press client should connects to the TCP server. I have tryed it as the code below, but i`m getting FATAL EXCEPTION: AsyncTask #1 error. please can you look at my code! Thanks.</p>
  
  <pre><code>    // ----------------------- CONNECTION BUTTON EVENTLISTENER - begin ----------------------------
  Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener(){
  @Override
  public void onClick(View arg0) {
  
  
  
  class connectTask extends AsyncTask&lt;String, String, AndroidClientActivity&gt; {
  @Override
  protected AndroidClientActivity doInBackground(String... message) {
  
  
  
  
  
      if(!connected){//if not connected yet
      outputText("connecting to Server");
      try {//try to create a socket and outputstream
      socket = new Socket("192.168.1.112",5353);//create a socket
      dataOutputStream = new DataOutputStream(socket.getOutputStream());//and stream
      outputText("successfully connected");//output the connection status
      changeConnectionStatus(true);//change the connection status
      } catch (UnknownHostException e) {//catch and
      outputText(e.getMessage());//display errors
      changeConnectionStatus(false);
      } catch (IOException e) {//catch and
      outputText(e.getMessage());//display errors
      changeConnectionStatus(false);
      }
      }else{
      outputText("disconnecting from Server...");
      try {//try to close the socket
      socket.close();
      outputText("successfully disconnected");
      changeConnectionStatus(false);//change the connection status
      } catch (UnknownHostException e) {//catch and
      outputText(e.getMessage());//display errors
      } catch (IOException e) {//catch and
      outputText(e.getMessage());//display errors
      }
      }
  
  
  
  
      return null;
      }
  
      @Override
      protected void onPostExecute(AndroidClientActivity jh) {
  
  
  
      }}
                  new connectTask().execute();            
              }
          };
  </code></pre>
  
  <p>and this is the error i geting;</p>
  
  <pre><code>04-23 23:27:30.445: E/AndroidRuntime(12005): FATAL EXCEPTION: AsyncTask #1
  04-23 23:27:30.445: E/AndroidRuntime(12005): java.lang.RuntimeException: An error occured while executing doInBackground()
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.lang.Thread.run(Thread.java:856)
  04-23 23:27:30.445: E/AndroidRuntime(12005): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.view.View.requestLayout(View.java:12863)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.view.View.requestLayout(View.java:12863)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.view.View.requestLayout(View.java:12863)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.view.View.requestLayout(View.java:12863)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:273)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.view.View.requestLayout(View.java:12863)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.widget.TextView.checkForRelayout(TextView.java:6985)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.widget.TextView.setText(TextView.java:3333)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.widget.TextView.setText(TextView.java:3189)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.widget.TextView.append(TextView.java:2849)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.widget.TextView.append(TextView.java:2839)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at com.rovercontrollerandroid.AndroidClientActivity.outputText(AndroidClientActivity.java:249)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at com.rovercontrollerandroid.AndroidClientActivity$3$1connectTask.doInBackground(AndroidClientActivity.java:192)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at com.rovercontrollerandroid.AndroidClientActivity$3$1connectTask.doInBackground(AndroidClientActivity.java:1)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  04-23 23:27:30.445: E/AndroidRuntime(12005):    ... 5 more
  </code></pre>