我使用以下代码
创建了一个AsyncTask类<div class="offerButtons">
<button type="reset" class="btnReset"><span> No </span></button>
<input type="text" class="offerInput" />
<button type="submit" class="btnSubmit"><span> Yes </span></button>
</div>
我第一次尝试: 我第一次看到ProgressDialog,但第二次打开活动时我什么都没得到。
我第二次尝试: 即使是第一次尝试,我也没有得到ProgressDialog。
我在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);
}
}
有人知道它为什么不起作用吗?请帮帮我。
答案 0 :(得分:1)
您的对话框一旦显示就会被解除,因为您的doInBackground
为空。尝试添加Thread.sleep()
几秒钟,只是为了模拟延迟。
另外,我怀疑你开始的新活动会让对话落后。因此,我建议您暂时不使用这些新活动来测试代码。
public class RemoveDialog extends AsyncTask<Void, Void, Void> {
ProgressDialog asyncDialog;
public RemoveDialog(Context c) {
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();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(3000);
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
//hide the dialog
asyncDialog.dismiss();
super.onPostExecute(result);
}
}