title: Java Thread CallBack in android
body: <p>For whatever reason I can't get my callback working.  Essentially I'm going to be making a group chat using a tcp server that I created.  I'm trying to create a thread in java and then use a callback to provide the information that I got from the socket back to the main activity.  </p>
  
  <p>The error I'm currently receiving:</p>
  
  <pre><code>12-04 02:53:05.969  15143-15169/com.AndroidBabble E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-4340
  java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  </code></pre>
  
  <p>But i'm not sure how to do this looper.prepare, and when I do it it doesn't seem to be working - so i'm wondering how you implement looper.prepare in the context of my code.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public interface ResponseEvent {
    public void responseEvent();
  }    
  </code></pre>
  
  <p><strong>Main</strong></p>
  
  <pre><code>public class Main extends Activity implements ResponseEvent{
    Button btn;
    ClientTask client;
    String LOCALHOST = "localhost";
    int PORT = 5678;
    Thread thread;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      btn = (Button) findViewById(R.id.button);
  //    Thread thread = new Thread(client = new ClientTask(this, LOCALHOST, PORT));
      client = new ClientTask(this, LOCALHOST, PORT);
  
    }
  
    public void button(View v){
      thread = new Thread(client);
      thread.start();
  
    }
  
    public void responseEvent(){
      Toast.makeText(getBaseContext(), "Trig", Toast.LENGTH_LONG).show();
    }
  
  
  }
  </code></pre>
  
  <p><strong>ClientTask</strong></p>
  
  <pre><code>public class ClientTask implements Runnable{
    public Handler mHandler;
    private ResponseEvent re;
    BufferedReader in;
    Socket socket;
    String HOSTNAME;
    int PORT;
    String scan;
  
    public ClientTask(ResponseEvent event, String HOSTNAME, int PORT){
      re = event;
      this.HOSTNAME = HOSTNAME;
      this.PORT = PORT;
    }
  
    public void run(){
   //   try {
    //    socket = new Socket(HOSTNAME, PORT);
     //   in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
        re.responseEvent();
  //    } catch (IOException ex) {
      }
    }
  </code></pre>
  
  <p>Eventually I want to pass a string through the response event and it will get it in the main.</p>