[英]Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog
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? 有什么想法吗?
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()
,而doInBackground()
在不同的线程上运行,这是AsyncTask
设计的主要原因。
You have to call show()
either in onProgressUpdate()
or in onPostExecute()
. 您必须在
onProgressUpdate()
或onPostExecute()
调用show()
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();
}
}
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. 似乎为我做了伎俩。
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();
}
});
}
}
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.