【发布时间】:2016-11-17 10:41:51
【问题描述】:
我用下面的代码做了一个 AsyncTask 类
public class removeDialog extends AsyncTask<Void, Void, Void> {
Context c;
ProgressDialog asyncDialog;
String page;
public removeDialog(Context c, String page) {
this.c = c;
this.page = page;
asyncDialog = new ProgressDialog(c);
}
@Override
protected void onPreExecute() {
//set message of the dialog
asyncDialog.setTitle("Please wait");
asyncDialog.setMessage("Loading...");
asyncDialog.setCancelable(false);
//show dialog
asyncDialog.show();
if (page == "algemeneVoorwaarden") {
Intent intent = new Intent(c, algemeneVoorwaarden.class);
c.startActivity(intent);
}
if (page == "contact") {
Intent intent = new Intent(c, contactTest.class);
c.startActivity(intent);
}
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
//don't touch dialog here it'll break the application
//do some lengthy stuff like calling login webservice
return null;
}
@Override
protected void onPostExecute(Void result) {
//hide the dialog
asyncDialog.dismiss();
super.onPostExecute(result);
}
}
我第一次尝试: 第一次看到 ProgressDialog,但第二次我想打开 Activity 时却一无所获。
我第二次尝试: 我第一次尝试时也没有 ProgressDialog。
我在 AsyncTask 类中执行我的代码,代码:
voorwaarden.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new removeDialog(c, "algemeneVoorwaarden").execute();
}
});
有人知道为什么它不起作用吗?请帮帮我。
【问题讨论】:
-
removeDialog是您的 AsyncTask 派生类的名称吗?如果你能发布你的班级的标题会很好。 -
是类名。
-
好的。好吧,在您的
onPreExecute中,您正在显示对话框并开始一个新活动。我可能是错的,但我认为你的对话框显示在第一个活动中,所以当你开始一个新的活动时,你的对话框被留下了。 -
嗯,你知道怎么做吗?
-
我尝试了:ThreadSleep,我得到了一些进一步的结果。 ProgressDialog 正在后台运行,但我不知道如何修复它。
标签: java android-activity android-asynctask progressdialog