我有以下代码
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);
}
}发布于 2010-11-19 01:30:33
长篇大论:
AsyncTask在内部使用Handler。处理程序基本上允许您从该处理程序所分配到的线程上的另一个线程发布Runnables,在AsyncTask的情况下,该线程始终是从中调用它的线程。不过,这只适用于准备了Looper的线程。
有关更多信息,请参阅http://developer.android.com/reference/android/os/Handler.html
短篇故事:
只需将对FinderMain$1.gotLocation的每个调用或在其中创建AsyncTask的每个调用包装在Runnable中,并将其发布到绑定到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);
}
});
// ...
}
}发布于 2011-03-14 00:26:45
我试过this...It工作,希望它能对你有所帮助。
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;
}
}https://stackoverflow.com/questions/4187960
复制相似问题