title: using handler inside a thread android
body: <p>i am trying to run a handler inside a thread but i'm getting the error "Can't create handler inside thread that has not called Looper.prepare()" i have spent hours looking for a resolution but couldnt find one so i decided to post this. i have tried calling "<strong>Looper.prepare();</strong>" it fixed the force close problem but stops the code inside handler from working.</p>
  
  <pre><code>    public void ten(View view) {
          Timer timer = new Timer();
          timer.schedule(new TimerTask() {
              private Vibrator mVibrator;
              private Runnable runnable;
              private Handler handler;
  
              public void run() {
                  mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                  mVibrator.vibrate(1000 * 10);// 10 sec sprint
  
                  // HANDLER
                  handler = new Handler();
                  runnable = new Runnable() {
                      public void run() {
                          // calculate result1
                          TextView theFact = (TextView) findViewById(R.id.txtCurrentSpeed);
                          String shareFact = theFact.getText().toString();
                          TextView theFact1 = (TextView) findViewById(R.id.result1);
                          theFact1.setText(String.valueOf(shareFact));
                          // calculate result1
                      }
                  };
                  handler.postDelayed(runnable, 3000);
                  // HANDLER END //
              }
          }, 5000, 60 * 1000 * 3);// 3 minute break (+5s first run delay)
      }
  }
  
  
  
  08-10 21:37:59.187: E/AndroidRuntime(26129): FATAL EXCEPTION: Timer-0
  08-10 21:37:59.187: E/AndroidRuntime(26129): Process: com.example.speedometer, PID: 26129
  08-10 21:37:59.187: E/AndroidRuntime(26129): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  08-10 21:37:59.187: E/AndroidRuntime(26129):    at android.os.Handler.&lt;init&gt;(Handler.java:200)
  08-10 21:37:59.187: E/AndroidRuntime(26129):    at android.os.Handler.&lt;init&gt;(Handler.java:114)
  08-10 21:37:59.187: E/AndroidRuntime(26129):    at com.example.speedometer.MainActivity$2.run(MainActivity.java:141)
  08-10 21:37:59.187: E/AndroidRuntime(26129):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  </code></pre>
  
  <p><strong>i replaced the Handler with Thread, the code inside the thread worked, however it force closes giving the following exception:</strong></p>
  
  <pre><code>08-10 21:57:35.477: E/AndroidRuntime(31261): FATAL EXCEPTION: Thread-4399
  08-10 21:57:35.477: E/AndroidRuntime(31261): Process: com.example.speedometer, PID: 31261
  08-10 21:57:35.477: E/AndroidRuntime(31261): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7147)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:998)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.view.View.requestLayout(View.java:18491)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.view.View.requestLayout(View.java:18491)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.view.View.requestLayout(View.java:18491)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.view.View.requestLayout(View.java:18491)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:361)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.view.View.requestLayout(View.java:18491)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.widget.TextView.checkForRelayout(TextView.java:8060)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.widget.TextView.setText(TextView.java:4836)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.widget.TextView.setText(TextView.java:4660)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at android.widget.TextView.setText(TextView.java:4635)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at com.example.speedometer.MainActivity$2$1.run(MainActivity.java:154)
  08-10 21:57:35.477: E/AndroidRuntime(31261):    at java.lang.Thread.run(Thread.java:818)
  </code></pre>