循环中的异步任务?
我正在尝试在我的应用程序中实现AsyncTask
。问题是它是从不同的线程创建和执行的,所以我得到了异常。我继续前进并实现了小型可运行程序,它将创建并执行我的 AsyncTask。我在 runOnUIThread() 方法中运行此可运行对象,但在可运行对象的构造函数中仍然遇到此错误,与 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();
}
}
I'm trying to implement AsyncTask
in my application. Problem is that it was created and executed from different thread, so I got exception. I moved forward and implement small runnable that will create and execute my AsyncTask. I run this runnable in runOnUIThread()
method, but still got this error in my runnable's constructor, on line with AsyncTask
constructor:
Can't create handler inside thread that has not called `Looper.prepare()`
Any ideas what to do?
Need code?
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AsyncTask 的构造函数仍在非 UI 线程上调用。您可以将 AsyncTask 的构造移至 run 方法吗?
The constructor of the AsyncTask is still being called on the non-UI thread. Can you move the construction of the AsyncTask to run method?