java.lang.RuntimeException:Looper.prepare()的问题

时间:2013-06-05 12:07:20

标签: android multithreading looper

我在我的应用程序中使用了AsyncTask,代码是,

protected Void doInBackground(Void... params) {
        // Get the current thread's token

        try {
            synchronized (this) {
                Looper.prepare();                   
                if (isCancelled()) {

                } else {                        
                    gpsCoordinates = new GetGpsCoordinates();   
                    location = gpsCoordinates.getLocation(context);

                    int counter = 0;
                    while (counter <= 4) {
                        this.wait(850);
                        counter++;
                        publishProgress((int) (counter) * 50);
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

和GetGpsCoordinates.class,

public Location getLocation(Context mContext) {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        // 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
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            Log.d("","collecting lat,lng details");
            if (isNetworkEnabled) {             
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // 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, 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();

                        }
                    }
                }
            }
        }

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

    return location;
}

如果我使用looper.prepare(),我会得到一些感觉。  这是我的logcat,

 06-05 17:26:16.410: E/AndroidRuntime(19075): java.lang.RuntimeException: An error occured while executing doInBackground()
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at android.os.AsyncTask$3.done(AsyncTask.java:200)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at  java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at java.lang.Thread.run(Thread.java:1019)
 06-05 17:26:16.410: E/AndroidRuntime(19075): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at android.os.Looper.prepare(Looper.java:74)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at com.bu.PropertySearchTypes.CameraSearch$LoadViewTask.doInBackground(CameraSearch.java:125)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at com.bu.PropertySearchTypes.CameraSearch$LoadViewTask.doInBackground(CameraSearch.java:1)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
 06-05 17:26:16.410: E/AndroidRuntime(19075):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

我在onSensorChanged()中调用此asynctask,即,对于每个传感器值更改,执行此线程。 请帮帮我。我对这个例外感到非常沮丧。 谢谢你的帮助!!

1 个答案:

答案 0 :(得分:1)

你必须在新线程中启动新的循环器。该例外包含此信息。要正确使用,请查看以下帖子:What is the purpose of Looper and how to use it?

但真正的问题是你是否需要它?如果你想启动一个处理程序,你需要它。我不确定你是否需要它,但即使你需要它,也有专门的HandlerThread课程来处理Looper。