Lee*_*ong 31 android looper android-asynctask
我有以下代码
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎在一些测试设备上工作正常,但我发现在开发控制台上出现了很多错误.我似乎无法弄清楚为什么以及在哪里放置这个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)
Run Code Online (Sandbox Code Playgroud)
按要求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);
}
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*Ohr 95
很长的故事:
AsyncTask内部使用a Handler.处理程序基本上允许您Runnables从处理程序分配给的线程上的另一个线程发布,在这种情况下,AsyncTask它始终是调用它的线程.但这仅适用于已Looper准备好的线程.
有关更多信息,请参阅http://developer.android.com/reference/android/os/Handler.html
短篇故事:
只需将每个调用FinderMain$1.gotLocation或其中的创建包装AsyncTask在一个中Runnable,然后将其发布到Handler绑定到UI线程,如下所示:
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);
}
});
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
Nil*_*upe 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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49440 次 |
| 最近记录: |