无法在未调用looper.prepare()asynctask的线程内创建处理程序

时间:2012-09-26 07:54:02

标签: android android-asynctask android-location locationlistener

我使用以下异步类来显示对话框并获取用户的当前位置,但收到上述错误。

ProgressDialog progressDialog;

class LoadJsonTask extends AsyncTask<Void, Void, Void> {
      protected void onPreExecute() {
      progressDialog = ProgressDialog.show(NewTraveler.this, "", "Loading...");
    }

@Override
protected Void doInBackground(Void... arg0) {
try {
         // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this
                    .getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();

                System.out.println("Latitude:- " + latitude);
                System.out.println("Longitude:- " + longitude);
        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    // Register the listener with the Location Manager to receive
    // location
    // updates
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

        } catch (Exception e) {
                System.out.println("error ".getMessage());
            }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }
}

在我的onCreate方法中,我使用
来调用上面的类 new LoadJsonTask()。execute();

请帮帮我。

1 个答案:

答案 0 :(得分:1)

使用此

// Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();

                System.out.println("Latitude:- " + latitude);
                System.out.println("Longitude:- " + longitude);
        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };
在doInBackground方法之外。