我有以下代码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.ExceptionInInitializerErrorat 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; }
3 回答
Qyouu
TA贡献1786条经验 获得超11个赞
所有对gotLocation的调用都需要在UI线程中进行,即在上述Runnable中进行。您也可以将整个部分包装在其中,但是请记住,如果执行时间过长,这可能会降低UI的速度。另外,您也可以将AsyncTask创建内容包装在类似Runnable的gotLocation中。
- 3 回答
- 0 关注
- 504 浏览
添加回答
举报
0/150
提交
取消