我正在尝试向用户显示登录对话框(带有自定义视图的自定义对话框),然后单击“登录”时,我正在尝试显示 ProgressDialog,然后在登录完成后将其关闭。
问题是我无法显示 ProgressDialog。
这是我的代码:
private final MyActivity activity;
public MyPresenter(MyActivity activity) {
this.activity = activity;
}
public Dialog getLoginSignupDialog() {
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.dialog_signup);
dialog.setTitle("Login");
final TextView email = (TextView)dialog.findViewById(R.id.textViewEmail);
final TextView password = (TextView)dialog.findViewById(R.id.textViewPassword);
Button signupButton = (Button)dialog.findViewById(R.id.buttonSignup);
signupButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ProgressDialog progressDialog = null;
try {
progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
activity.signup(email.getText().toString(), password.getText().toString());
progressDialog.dismiss();
dialog.dismiss();
} catch (ParseException e) {
if (progressDialog != null) progressDialog.dismiss();
Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
return dialog;
}
这段代码在一个 Presenter 中,它传递了主要的活动实例。只是为了保持 UI 逻辑独立。
在主要活动(onCreate
)中,我这样做:
showDialog(DIALOG_SIGNUP);
然后我覆盖onCreateDialog
方法:
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DIALOG_SIGNUP: {
dialog = presenter.getLoginSignupDialog();
break;
}
}
return dialog;
}
另一个对话框(自定义登录)显示正常,一切正常,关闭等。但我无法显示 ProgressDialog。
我究竟做错了什么?