Java 无法在 AsyncTask 中为 ProgressDialog 调用 Looper.prepare() 的线程内创建处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3614663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog
提问by mlevit
I don't understand why I'm getting this error. I'm using AsyncTask to run some processes in the background.
我不明白为什么我会收到这个错误。我正在使用 AsyncTask 在后台运行一些进程。
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:
当我进入doInBackground()
取决于条件时,我:
[...]
connectionProgressDialog.dismiss();
downloadSpinnerProgressDialog.show();
[...]
Whenever I try downloadSpinnerProgressDialog.show()
I receive the error.
每当我尝试时,downloadSpinnerProgressDialog.show()
我都会收到错误消息。
Any ideas guys?
有什么想法吗?
采纳答案by Konstantin Burov
The method show()
must be called from the User-Interface (UI) thread, while doInBackground()
runs on different thread which is the main reason why AsyncTask
was designed.
该方法show()
必须从用户界面 (UI) 线程调用,同时doInBackground()
在不同的线程上运行,这是AsyncTask
设计的主要原因。
You have to call show()
either in onProgressUpdate()
or in onPostExecute()
.
您必须调用show()
inonProgressUpdate()
或 in onPostExecute()
。
For example:
例如:
class ExampleTask extends AsyncTask<String, String, String> {
// Your onPreExecute method.
@Override
protected String doInBackground(String... params) {
// Your code.
if (condition_is_true) {
this.publishProgress("Show the dialog");
}
return "Result";
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
connectionProgressDialog.dismiss();
downloadSpinnerProgressDialog.show();
}
}
回答by hyui
I had a similar issue but from reading this question I figured I could run on UI thread:
我有一个类似的问题,但从阅读这个问题我想我可以在 UI 线程上运行:
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
alertDialog.show();
}
});
Seems to do the trick for me.
似乎对我有用。
回答by jayesh kavathiya
final Handler handler = new Handler() {
@Override
public void handleMessage(final Message msgs) {
//write your code hear which give error
}
}
new Thread(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(1);
//this will call handleMessage function and hendal all error
}
}).start();
回答by caiocpricci2
I had a hard time making this work too, the solution for me was to use both hyui and konstantin answers,
我也很难完成这项工作,我的解决方案是同时使用 hyui 和 konstantin 的答案,
class ExampleTask extends AsyncTask<String, String, String> {
// Your onPreExecute method.
@Override
protected String doInBackground(String... params) {
// Your code.
if (condition_is_true) {
this.publishProgress("Show the dialog");
}
return "Result";
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
alertDialog.show();
}
});
}
}