AsynchTask:只有一条环线可以每个线程创建 [英] AsynchTask:Only one Looper may be created per thread

查看:250
本文介绍了AsynchTask:只有一条环线可以每个线程创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序崩溃的每4-5th电话。它显示我以下异常。

My app crashing on every 4-5th call. It's showing me following exception.

10-14 14:00:30.651: E/AndroidRuntime(25035): FATAL EXCEPTION: AsyncTask #1
10-14 14:00:30.651: E/AndroidRuntime(25035): java.lang.RuntimeException: An error occured while executing doInBackground()
10-14 14:00:30.651: E/AndroidRuntime(25035):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at java.lang.Thread.run(Thread.java:838)
10-14 14:00:30.651: E/AndroidRuntime(25035): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
10-14 14:00:30.651: E/AndroidRuntime(25035):    at android.os.Looper.prepare(Looper.java:80)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at android.os.Looper.prepare(Looper.java:75)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at com..SplashActivity$LocationUpdator.doInBackground(SplashActivity.java:118)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at com..SplashActivity$LocationUpdator.doInBackground(SplashActivity.java:1)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-14 14:00:30.651: E/AndroidRuntime(25035):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)  

下面是我的code。

Here is my code.

public class LocationUpdator extends AsyncTask<Void, Void, Location> {
 Location location123 = null;



@Override
protected Location doInBackground(Void... arg0) {

    Looper.prepare();
    mThreadLooper = Looper.myLooper();          
    locationListener = new LocationListener() 
     {
     private String subLocality;
    @Override 
     public void onLocationChanged(Location location1) { 


         if(mThreadLooper != null)
            mThreadLooper.quit();
         //Stopping Request for Location
         if(manager!=null)
             manager.removeUpdates(locationListener);
     } 

    String status="success";


        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            // if GPS provider is not enable then popup alertbox
            buildAlertMessageNoGps();
        } else {        
            // if gps is one then start searching                    
            status = "networkandgps";
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000,1000,  locationListener); 
        }
    }

        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Log.i("TEST","Starting Network Provider");
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10,locationListener);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            //buildAlertMessageNoGps();
            ;
        } else {
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100000,100000,locationListener);               
        }
    } else {           

        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10,locationListener);            
    }

     Looper.loop();
    return location123;
}

@Override
protected void onPostExecute(Location result) {


    //progressDialog.dismiss();
    if(result!=null)
    {               
        Log.i("TESTING SPLASH","Lat3 "+userLocationLat);
        Log.i("TESTING SPLASH ","Long3 "+userLocationlong);

        startApplication();         

    }           

}
 }

} }

我也尝试戒烟环路的onStop方法。但没有帮助。我知道它怪异使用尺蠖在非同步任务,但没有任何其他方式使用非同步任务的doInBackground方法内部位置监听器。

I also try to quit loop in onstop method. but not helpful. I know it weird to use looper in Asynch task but is there any another way to use location listener inside Asynch Task's doInBackground method.

请给我暗示为什么我收到此错误。

Please give me hint why i am getting this error.

推荐答案

它的崩溃,因为的AsyncTask的螺纹用作线程池(这意味着它的使用相同的4个或5个线程和再次使用。据说,第1部分的回答是:

it's crashing because the threads of AsyncTask are used as a ThreadPool (that means it's the same 4 or 5 threads used and re-used. Said that the 1st part of the answer is:

没有!你不能用一个活套里面的AsyncTask因为只有一条环线可以每个线程创建

No! You cannot use a Looper inside a AsyncTask because Only one Looper may be created per thread

在第二个想法,你为什么非得用这个活套?这不是在做你的code什么。如果您认为的缘故吧,监听器被称为后台线程,我有坏消息。不是这样!侦听器将要调用哪个线程调用,通常是这样的UI线程。如果你想避免在UI线程长时间运行的进程(如网络),你应该开始从公共无效的一个新的线程(或新的AsyncTask)onLocationChanged(位置)方法。把监听器code中的受保护的位置doInBackground(虚空......为arg0)内只让你的code非常混乱,在实际执行无差异阅读

On second thought, why are you using this Looper anyway? It is not doing anything in your code. If you believe that because of it, the Listener is being called in the background thread, I have bad news. It is not! The listener will be called in whichever thread it wants to call, normally that is the UI-Thread. If you want to avoid long running processes (such as network) in the UI thread, you should start a new thread (or a new AsyncTask) from the public void onLocationChanged(Location) method. Putting the Listener code inside the protected Location doInBackground(Void... arg0) is only making your code very confusing to read with no difference in actual execution.

我希望它能帮助。

编辑:

在搭载Android 4.0(或3.0,不知道),最多是一个SingleThreadPool,这使得这个乱用活套越紧了一下。

in Android 4.0 (or 3.0, not sure) and up it's a SingleThreadPool, which makes this mess with Loopers a bit more tight.

这篇关于AsynchTask:只有一条环线可以每个线程创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆