我不明白为什么我得到这个错误。我使用的AsyncTask运行在后台的一些过程。
I don't understand why I'm getting this error. I'm using AsyncTask to run some processes in the background.
我有:
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()
我收到错误。
任何想法家伙?
方法显示()
必须从的用户界面(UI)线程,而 doInBackground()
运行在不同的线程这是最主要的原因的AsyncTask
设计。
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.
您必须调用显示()
无论是在 onProgressUpdate()
或 onPostExecute ()
。
You have to call show()
either in onProgressUpdate()
or in onPostExecute()
.
例如:
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();
}
}