无法在未在 AsyncTask 中为 ProgressDialog 调用 Looper.prepare() 的线程内创建处理程序
我不明白为什么我会收到此错误。我正在使用 AsyncTask 在后台运行一些进程。
我有:
protected void onPreExecute()
{
connectionProgressDialog = new ProgressDialog(SetPreference.this);
connectionProgressDialog.setCancelable(true);
connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
connectionProgressDialog.setMessage("Connecting to site...");
connectionProgressDialog.show();
downloadSpinnerProgressDialog = new ProgressDialog(SetPreference.this);
downloadSpinnerProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downloadSpinnerProgressDialog.setMessage("Downloading wallpaper...");
}
当我根据条件进入 doInBackground()
时,我:
[...]
connectionProgressDialog.dismiss();
downloadSpinnerProgressDialog.show();
[...]
每当我尝试 downloadSpinnerProgressDialog.show()
时,我都会收到错误。
大家有什么想法吗?
I don't understand why I'm getting this error. I'm using AsyncTask to run some processes in the background.
I have:
protected void onPreExecute()
{
connectionProgressDialog = new ProgressDialog(SetPreference.this);
connectionProgressDialog.setCancelable(true);
connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
connectionProgressDialog.setMessage("Connecting to site...");
connectionProgressDialog.show();
downloadSpinnerProgressDialog = new ProgressDialog(SetPreference.this);
downloadSpinnerProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downloadSpinnerProgressDialog.setMessage("Downloading wallpaper...");
}
When I get into doInBackground()
depending on a condition I:
[...]
connectionProgressDialog.dismiss();
downloadSpinnerProgressDialog.show();
[...]
Whenever I try downloadSpinnerProgressDialog.show()
I receive the error.
Any ideas guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法
show()
必须从 用户界面 (UI) 线程 调用,而doInBackground()
运行在不同的线程上,这是设计AsyncTask
的主要原因。您必须在
onProgressUpdate()
或onPostExecute()
中调用show()
。例如:
The method
show()
must be called from the User-Interface (UI) thread, whiledoInBackground()
runs on different thread which is the main reason whyAsyncTask
was designed.You have to call
show()
either inonProgressUpdate()
or inonPostExecute()
.For example:
我有一个类似的问题,但通过阅读这个问题,我认为我可以在 UI 线程上运行:
似乎对我有用。
I had a similar issue but from reading this question I figured I could run on UI thread:
Seems to do the trick for me.
我也很难完成这项工作,对我来说,解决方案是同时使用 hui 和 konstantin 答案,
I had a hard time making this work too, the solution for me was to use both hyui and konstantin answers,