编辑:问题解决了。让它全球化。我现在正在做一个facepalm。谢谢!
以下是扩展AsyncTask的类的一些片段。我想在preExecute()方法中启动progressDialog并执行progressDialog.dismiss();在postExecute()方法中。我发现的所有例子都说如下所示。我遇到的问题是对话框超出了onPostExecute的范围。这是预期的,但所有的例子似乎都是这样做的。我还注意到在导入时有一个警告标志,表示导入未使用。这个ProgressDialog应该有效吗?我需要传递它吗?
import android.app.ProgressDialog;
...
protected void onPostExecute(Bitmap image){//error when doing this in resetDisplay.... onPostExecute is invoked by the ui thread so this may be why it works here and not in resetDisplay
ImageView imageView=(ImageView) parent.findViewById(R.id.imageDisplay);
imageView.setImageBitmap(image);
dialog.dismiss();
}
protected void onPreExecute(){
ProgressDialog dialog=ProgressDialog.show(parent, "Loading", "Loading the image of the day");
}
答案 0 :(得分:2)
请参阅以下代码
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);
/** progress dialog to show user that the backup is processing. */
/** application context. */
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
protected Boolean doInBackground(final String... args) {
try {
/**
* Fetch the RSS Feeds from URL
*/
Utilities.arrayRSS = objRSSFeed
.FetchRSSFeeds(Constants.Feed_URL);
return true;
} catch (Exception e) {
Log.e("tag", "error", e);
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
// display UI
UpdateDisplay();
}
}
}
答案 1 :(得分:1)
class YourClass extends AsyncTask<...>
{
ProgressDialog dialog;
Protected void onPreExecute(){
// create dialog here
dialog= new ProgressDialog (...);
}
protected void onPostExecute(Bitmap image){
//
dialog.dismiss();
}
}
答案 2 :(得分:0)
您需要在ProgressDialog dialog
之外声明onPreExecute()
在活动中声明对话框,然后在preExecute()和postExecute()方法中使用
在teh活动中或在异步任务中声明如下
ProgressDialog dialog=new Progress Dialog(YourActivity.this);
然后使用onPreExecute()中的对话框作为
dialog.setMessage("Your Messgae");
dialog.show();
和onPostExecute()为
dialog.dismiss();