AsyncTask和Looper.prepare()在每第6次调用时崩溃

时间:2013-08-15 06:48:51

标签: android android-asynctask looper

我在doInBackground()中遇到了looper.prepare()的问题。如果我不使用looper.prepare它会生成一个异常"不能在线程内创建一个没有调用Looper.prepare()"当我使用looper.prepare时,它适用于5次调用looper.prepare,并且在第六次调用时它会生成以下异常。

08-15 11:29:48.078: W/System.err(14133): java.lang.RuntimeException: Only one Looper may be created per thread
08-15 11:29:48.088: W/System.err(14133):    at android.os.Looper.prepare(Looper.java:74)
08-15 11:29:48.088: W/System.err(14133):    at the.bike.washer.SelectLocation$setUserCurrentLocation.doInBackground(SelectLocation.java:1504)
08-15 11:29:48.088: W/System.err(14133):    at the.bike.washer.SelectLocation$setUserCurrentLocation.doInBackground(SelectLocation.java:1)
08-15 11:29:48.088: W/System.err(14133):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-15 11:29:48.088: W/System.err(14133):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 11:29:48.088: W/System.err(14133):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 11:29:48.088: W/System.err(14133):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-15 11:29:48.088: W/System.err(14133):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-15 11:29:48.088: W/System.err(14133):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-15 11:29:48.088: W/System.err(14133):    at java.lang.Thread.run(Thread.java:856)

这是代码

try {
                Looper.prepare();
                locationManager = (LocationManager) context
                        .getSystemService(LOCATION_SERVICE);

                // getting GPS status
                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);

                // getting network status
                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (!isGPSEnabled && !isNetworkEnabled) {
                    // no network provider is enabled
                    double lat = 48.8742;
                    double lng = 2.3470;
                    Constants.coordinates = new LatLng(lat, lng);
                    Constants.address = getFromLocation(lat, lng, 1);
                    result = "no gps";
                } else {
                    //this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, SelectLocation.this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                Constants.coordinates = new LatLng(latitude, longitude);
                                Constants.address = getFromLocation(latitude, longitude, 1);
                            }
                        }
                    }
                    // if GPS Enabled get lat/long using GPS Services
                    if (isGPSEnabled) {
                        if (location == null) {
                            locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, SelectLocation.this);
                            Log.d("GPS Enabled", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                    Constants.coordinates = new LatLng(latitude, longitude);
                                    Constants.address = getFromLocation(latitude, longitude, 1);
                                }
                            }
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

0 个答案:

没有答案