title: AsyncTask to get Location in android go wrong with "can't create handler inside thread that has not called looper.prepare()"
body: <p>all</p>
  
  <p>I have carefully read many related Q&amp;A in this site, but nothing help me. Here is my code, it works fine with GPS on, but went wrong once i turn off the GPS.</p>
  
  <pre><code>/**
   * 
   * @author Jimmy Chiang
   * 
   */
  public class GetLocationTask extends AsyncTask&lt;Context, Void, Location&gt; {
  
      private OnLocationListener[] mGpsListeners;
      private Location location;
  
      private LocationListener locationListener = new LocationListener() {
  
          @Override
          public void onLocationChanged(Location lo) {
              location = lo;
          }
  
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onProviderEnabled(String provider) {
              // TODO Auto-generated method stub
  
          }
  
          @Override
          public void onProviderDisabled(String provider) {
              // TODO Auto-generated method stub
  
          }
      };
  
      public interface OnLocationListener {
          public void onLocationGot(Location location);
      }
  
      public GetLocationTask setOnLocationListener(
              OnLocationListener... listeners) {
          mGpsListeners = listeners;
          return this;
      }
  
      @Override
      protected Location doInBackground(Context... params) {
  
  
          LocationManager locationManager = (LocationManager) params[0]
                  .getSystemService(Context.LOCATION_SERVICE);
  
          String provider = LocationManager.NETWORK_PROVIDER;
          if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  
              provider = LocationManager.GPS_PROVIDER;
  
          }
          location = locationManager.getLastKnownLocation(provider);
  
          while (location == null) {
  
              locationManager.requestLocationUpdates(provider, 1000, 1,locationListener);
          }
          return location;
      }
  
      @Override
      protected void onPostExecute(Location location) {
  
          for (OnLocationListener listener : mGpsListeners) {
              listener.onLocationGot(location);
          }
      }
  }
  </code></pre>
  
  <p>The exception shows:</p>
  
  <blockquote>
    <p>can't create handler inside thread that has not called
    looper.prepare()</p>
  </blockquote>
  
  <p>Some posts show that the problem should be call someting in UI Thread, but i can't find any of this. Thanks so much if you know what's going on~</p>