2010-11-15 35 views
31

我有以下代码的AsyncTask和Looper.prepare()错误

class OverlayTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    public void onPreExecute() { 

     if (sites != null) { 
      myMapView.getOverlays().remove(sites); 
      myMapView.invalidate(); 
      sites = null; 
     } 
    } 

    @Override 
    public Void doInBackground(Void... unused) { 
      grabShipsWithLocation(); 
      return (null); 
    } 

    @Override 
    public void onPostExecute(Void unused) { 
     myMapView.getOverlays().add(sites); 
     myMapView.invalidate(); 
     isLoading = false; 
    } 
} 

这似乎在一些测试设备工作正常,但我看到很多出现在开发者控制台上的错误。我似乎无法弄清楚为什么以及在哪里放置这个Looper.prepare()。需要吗?

java.lang.ExceptionInInitializerError 
at com.test.appname.FinderMain$1.gotLocation(FinderMain.java:286) 
at com.test.appname.MyLocation$GetLastLocation.run(MyLocation.java:89) 
at java.util.Timer$TimerImpl.run(Timer.java:289) 
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
at android.os.Handler.<init>(Handler.java:121) 
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421) 
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421) 
at android.os.AsyncTask.<clinit>(AsyncTask.java:152) 

按照要求MyLocation.java

class GetLastLocation extends TimerTask { 
    @Override 
    public void run() { 
     lm.removeUpdates(locationListenerGps); 
     lm.removeUpdates(locationListenerNetwork); 

     Location net_loc=null, gps_loc=null; 
     if(gps_enabled) 
      gps_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if(network_enabled) 
      net_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     //if there are both values use the latest one 
     if(gps_loc!=null && net_loc!=null){ 
      if(gps_loc.getTime()>net_loc.getTime()) 
       locationResult.gotLocation(gps_loc); 
      else 
       locationResult.gotLocation(net_loc); 
      return; 
     } 

     if(gps_loc!=null){ 
      locationResult.gotLocation(gps_loc); //Line 89 
      return; 
     } 
     if(net_loc!=null){ 
      locationResult.gotLocation(net_loc); 
      return; 
     } 
     locationResult.gotLocation(null); 
    } 
} 
+1

是什么grabShipsWithLocation()呢? – Falmarri 2010-11-15 19:21:22

+1

MyLocation.java:89是什么行?为您的来源添加评论。 – blindstuff 2010-11-15 19:22:05

+0

您是从主线程创建/启动AsyncTask吗? – gngr44 2010-11-15 20:00:48

回答

95

长的故事:

AsyncTask内部使用Handler。处理程序基本上可以让你从线程处理程序被分配到另一个线程,这在AsyncTask的情况下,总是从它被称为主题发表Runnables。但这只适用于准备Looper的线程。

欲了解更多信息,请参阅http://developer.android.com/reference/android/os/Handler.html

短篇小说:

简单包装每次调用FinderMain$1.gotLocation或在Runnable内它创造AsyncTask,并将其张贴到绑定到UI线程Handler ,像这样:

class GetLastLocation extends TimerTask { 
    private Handler mHandler = new Handler(Looper.getMainLooper()); 

    @Override 
    public void run() { 
     // ... 
     mHandler.post(new Runnable() { 
      public void run() { 
       locationResult.gotLocation(null); 
      } 
     }); 
     // ... 
    } 
} 
+0

Just the locationResult.gotLocation(null);在新的部分或上面的整个地段? – 2010-11-19 07:14:35

+0

或者每个locationResult.gotLocation(东西)?? – 2010-11-19 10:21:15

+0

所有对gotLocation的调用都需要在UI线程内发生,这意味着在上面的Runnable中。您也可以将整个部分包裹在其中,但请记住,如果执行时间过长,这可能会降低您的用户界面速度。另外,你也可以像这样在一个Runnable中将AsyncTask创建包装在gotLocation中。 – 2010-11-19 16:37:24

18

我试过这个...它的工作,希望它会帮助你..

protected class Asyctast extends AsyncTask<String, Integer, Integer> 
{ 

    @Override 
    protected Integer doInBackground(String... params) { 
     // TODO Auto-generated method stub 


     Log.d("Asynctask", ""+params); 
Looper.prepare(); 

     ImageThumbnailsActivity m = new ImageThumbnailsActivity(); 

      Toast.makeText(ImageThumbnailsActivity.this,""+params ,Toast.LENGTH_SHORT).show(); 
      final Dialog dialog_options = new Dialog(ImageThumbnailsActivity.this); 
      dialog_options.setContentView(R.layout.option); 
      dialog_options.show(); 
     Looper.loop(); 
     return null; 
    }  
} 
+7

这是一个没有很好的解决办法,导致Looper.loop()将永远封锁和线程永远不会完成。你应该使用Looper.myLooper()。quit()来避免这种情况。 除此之外,如果您需要从线程调用Looper.prepare() - 使用线程而不是AsyncTask。 – Borg8 2012-11-28 19:21:08

+1

删除Looper.loop()为我工作,以避免无限循环 – jgpATs2w 2013-06-17 21:43:27