Android ProgressDialog未显示

时间:2012-08-22 02:37:48

标签: android android-dialog

我正在尝试向用户显示一个登录对话框(带有自定义视图的自定义对话框),然后单击“登录”,我正在尝试显示ProgressDialog,然后在登录完成后关闭它。 / p>

问题是我无法显示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。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我认为将你的进度对话框放在你的函数activity.signup()中会有所帮助,并在那里将其解除。 这应该在主ui线程中创建对话框。

也许你应该在登录过程中使用AsyncTask。

答案 1 :(得分:0)

您应该将注册活动作为AsyncTask进行,如下所示:

    new AsyncTask<String, Void, Void>() {


        ProgressDialog progressDialog=null;


        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                activity.signup(params[0], params[1]);

            } catch (ParseException e) {
                Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog != null) progressDialog.dismiss();
            dialog.dismiss();
        }
    }.execute(email.getText().toString(), password.getText().toString());