title: Android - Can't create handler inside thread that has not called Looper.prepare()
body: <p>I am developing a simple application using Google Maps V2 API just to get basics and I am facing this error: </p>
  
  <pre><code>09-09 21:21:41.154: E/AndroidRuntime(3796): FATAL EXCEPTION: Thread-441
  09-09 21:21:41.154: E/AndroidRuntime(3796): java.lang.ExceptionInInitializerError
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at cz.vongrad.dataSourceLayer.DataMapper.loadLocation(DataMapper.java:111)
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at cz.vongrad.dataSourceLayer.DBFacade.loadLocations(DBFacade.java:32)
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at cz.vongrad.domainLayer.Controller.loadLocations(Controller.java:94)
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at cz.vongrad.locator.MapActivity$1.run(MapActivity.java:199)
  09-09 21:21:41.154: E/AndroidRuntime(3796): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at android.os.Handler.&lt;init&gt;(Handler.java:121)
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at android.os.AsyncTask$InternalHandler.&lt;init&gt;(AsyncTask.java:607)
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at android.os.AsyncTask$InternalHandler.&lt;init&gt;(AsyncTask.java:607)
  09-09 21:21:41.154: E/AndroidRuntime(3796):  at android.os.AsyncTask.&lt;clinit&gt;(AsyncTask.java:190)
  09-09 21:21:41.154: E/AndroidRuntime(3796):  ... 4 more
  09-09 21:21:41.164: E/EmbeddedLogger(239): App crashed!
  </code></pre>
  
  <p>DataMapper: </p>
  
  <pre><code>class LoadLocations extends AsyncTask&lt;String, Void, ArrayList&lt;Friend&gt;&gt;{
  
          private JSONObject jSONObject;
  
  
          @Override
          protected ArrayList&lt;Friend&gt; doInBackground(String... args) {
  
              List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("user_name", args[1]));
  
              jSONObject = jSONParser.makeHttpRequest(args[0], "POST", params);
              Log.d(TAG, jSONObject.toString());
  
              ArrayList&lt;Friend&gt; tempFriends = new ArrayList&lt;Friend&gt;();
  
              try {
                  if(jSONObject.getInt("success") == 1){
  
                      JSONArray jsonArray = jSONObject.getJSONArray("users");
  
  
                      for (int i = 0; i &lt; jsonArray.length(); i++) {
                          JSONObject o = jsonArray.getJSONObject(i);
  
                          tempFriends.add(new Friend(o.getString("user_name"), new LatLng(o.getDouble("loc_y"), o.getDouble("loc_x")), false));
                      }
  
                  }
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              return tempFriends;
          }
  
      }
  
  public ArrayList&lt;Friend&gt; loadLocation(String URL, String userName) throws InterruptedException, ExecutionException {
          return new LoadLocations().execute(URL, userName).get();
  
      }
  </code></pre>
  
  <p>MapActivity: </p>
  
  <pre><code>public class MapActivity extends Activity implements LocationSource, LocationListener {
  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  }
  private Thread loadFriendLocations = new Thread()
      {
  
          @Override
          public void run() {
              Log.d(TAG, "LastKnownLocation: " + locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
              try {
                  while(true) {
                      controller.loadLocations(LOAD_LOCATIONS);
                      Log.d(TAG, "Loading new locations");
                      Message msg = uIHandler.obtainMessage();
                      msg.what = ADD_MAP_PIN;
                      uIHandler.sendMessage(msg);
  
                      sleep(10000);                   
                  }
              } catch (InterruptedException e) {
                  e.printStackTrace();
              } catch (ExecutionException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      };
  
      final Handler uIHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
  
          if(msg.what==ADD_MAP_PIN){
  
              for (Friend friend : controller.getFriends()) {
  
                      addMapPin(friend);
  
  
              }
          }
          super.handleMessage(msg);
        }
      };
  </code></pre>
  
  <p>The weird thing is that I can run this app on my emulator (API 16) and it runs just great but when I try to run it on a physical device (API 15), it gets this error. The AsyncTask is run in separate class, so it might cause this problem. I would appreciate any suggestions. Thanks in advance. </p>