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);
}
}
I have the following code
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;
}
}
That seems to work fine on a few test devices but I am seeing a lot of errors appearing on the dev console. I can't seem to work out why and where to put this Looper.prepare(). Is it needed?
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)
As requested 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
长话短说:
AsyncTask
内部使用Handler
。处理程序基本上允许您从处理程序分配到的线程上的另一个线程发布 Runnables,在 AsyncTask 的情况下,该线程始终是调用它的线程。不过,这仅适用于准备了 Looper 的线程。有关详细信息,请参阅 http://developer.android.com/reference/android/os/Handler.html< /a>
短篇故事:
只需将对
FinderMain$1.gotLocation
的每次调用或在Runnable< 中创建
AsyncTask
的操作封装起来即可/code>,并将其发布到绑定到 UI 线程的Handler
,如下所示:Long story:
AsyncTask
internally uses aHandler
. A handler basically allows you to postRunnables
from another thread on the thread the handler was assigned to, which in the case ofAsyncTask
is always the thread from which it is called. This only works for threads that have aLooper
prepared, though.For more information see http://developer.android.com/reference/android/os/Handler.html
Short story:
Simply wrap every call to
FinderMain$1.gotLocation
or the creation ofAsyncTask
within it in aRunnable
, and post it to aHandler
bound to the UI thread, like this:我尝试过这个...它有效,希望它能帮助你..
I tried this...It worked,hope it will help you..