我正在尝试在我的应用程序中实现AsyncTask。问题是它是从不同的线程创建和执行的,所以我得到了异常。我前进了一步,实现了一个小的runnable,它将创建并执行我的AsyncTask。我在runOnUIThread()方法中运行了这个runnable,但在我的runnable的构造函数中,通过AsyncTask构造函数仍然得到了这个错误:
Can't create handler inside thread that has not called `Looper.prepare()`有什么好主意吗?
需要代码吗?
myLocationOverlay.runOnFirstFix(new Runnable() {
@Override
public void run() {
fillMap();
}
});
public void fillMap(){
runOnUiThread(new AsyncTaskRunner());
}
private class AsyncTaskRunner implements Runnable {
DownloadDataTask task;
public AsyncTaskRunner(double latitude, double longitude, int radius) {
super();
this.task = new DownloadDataTask(latitude, longitude, radius);
}
@Override
public void run() {
task.execute();
}
}发布于 2012-02-03 21:36:48
AsyncTask的构造函数仍在非UI线程上调用。您可以将AsyncTask的构造移动到run方法吗?
private class AsyncTaskRunner implements Runnable {
double latitude;
double longitude;
int radius;
public AsyncTaskRunner(double latitude, double longitude, int radius) {
super();
this.latitude = latitude;
this.longitude = longitude;
this.radius = radius;
}
@Override
public void run() {
new DownloadDataTask(latitude, longitude, radius).execute();
}
}https://stackoverflow.com/questions/9129356
复制相似问题