0

嗨,我在使用 Android 的 AsyncTask 类时遇到了一些麻烦。这是我在 Parse 上登录用户的代码。有不同的情况,例如密码不正确等。

我的问题是错误检查工作正常,不同情况下工作正常,但在我关闭警报对话框后,progressDialog 按钮不会消失......有人可以帮我吗?

这是我的代码

private class LoadTask extends AsyncTask<Map<String, String>, Integer, Void> {


        // called before running code in a separate thread

        private Result resultCode;
        private boolean isSuccess;
        private ProgressDialog progressDialog;


        public LoadTask(Activity activity) {
            onPreExecute();
        }
        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(TitlePage.this, 
            getString(R.string.login_progress_title),  
            getString(R.string.login_progress_message), false, false);
        }

        @Override
        protected Void doInBackground(Map<String, String>... arg0) {
            // Try to login with the given inputs
            ParseUser user = null;
            Map<String, String> argMap = arg0[0];
            try {
                user = ParseUser.logIn(argMap.get("username"), argMap.get("password"));
            } catch (ParseException e) {
                e.fillInStackTrace();
                boolean errorOccured = false;
                List<ParseObject> usernameResults = new ArrayList<ParseObject>();
                List<ParseObject> passwordResults = new ArrayList<ParseObject>();
                ParseQuery query = ParseUser.getQuery();
                // try to find the username that the user typed in
                query.whereEqualTo("username", argMap.get("username"));
                try {
                    query.count();
                    usernameResults = query.find();
                } catch (ParseException e1) {
                    // error occured trying to find the username
                    errorOccured = true;
                    e1.printStackTrace();
                } catch (NullPointerException e1) {
                    errorOccured = true;
                    e1.printStackTrace();
                }

                // try to find the password that the user typed in
                // associated with that username
                query.whereEqualTo("username", argMap.get("username"));
                query.whereEqualTo("password", argMap.get("password"));
                try {
                    query.count();
                    passwordResults = query.find();
                } catch (ParseException e1) {
                    // error occured trying to find the password
                    errorOccured = true;
                    e1.printStackTrace();
                } catch (NullPointerException e1) {
                    errorOccured = true;
                    e1.printStackTrace();
                }

                // figure out the error
                if (errorOccured) {
                    resultCode = Result.UNEXPECTED_ERROR;
                //  buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp);
                }
                if ((usernameResults.size() == 0) && (passwordResults.size() == 0)) {
                    resultCode = Result.BOTH_INCORRECT;
                //  buildAlertDialog(R.string.error_login_title, R.string.error_login_combo);
                } else if ((usernameResults.size() == 0) && (passwordResults.size() != 0)) {
                    resultCode = Result.USERNAME_INCORRECT;
                    //buildAlertDialog(R.string.error_login_title, R.string.error_login_uname);
                } else if ((usernameResults.size() != 0) && (passwordResults.size() == 0)) {
                    resultCode = Result.PASSWORD_INCORRECT;
                    //buildAlertDialog(R.string.error_login_title, R.string.error_login_pswd);
                } else {
                    // unexpected error
                    resultCode = Result.UNEXPECTED_ERROR;
                //  buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp);
                }
                isSuccess = false;
                return null;
            }
            // Check for verified email
            boolean emailVerified = user.getBoolean("emailVerified");
            if (!emailVerified) {
                resultCode = Result.EMAIL_NOT_VERIFIED;
                ParseUser.logOut();
            } 
            isSuccess = true;
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            progressDialog.dismiss();
            if (isSuccess) {
                TitlePage.this.setReturnStatus(isSuccess);
            } else {
                System.out.println("THIS IS RESULT CODE " + resultCode);
                if (resultCode == Result.UNEXPECTED_ERROR) {
                    buildAlertDialog(R.string.error_login_title, R.string.error_login_unexp);
                } else if (resultCode == Result.BOTH_INCORRECT) {
                    buildAlertDialog(R.string.error_login_title, R.string.error_login_combo);
                } else if (resultCode == Result.USERNAME_INCORRECT) {
                    buildAlertDialog(R.string.error_login_title, R.string.error_login_uname);
                } else if (resultCode == Result.PASSWORD_INCORRECT) { 
                    buildAlertDialog(R.string.error_login_title, R.string.error_login_pswd);
                } else {
                    buildAlertDialog(R.string.error_login_title, R.string.error_login_verif);
                }
                TitlePage.this.setReturnStatus(isSuccess);
            }
        }
    }

这是我在主要活动中运行异步任务的代码

Map<String, String> argMap = new HashMap<String, String>();
argMap.put("username", usernameString);
argMap.put("password", passwordString);
LoadTask task = new LoadTask(this);
task.execute(argMap);


private void buildAlertDialog(final int title, final int message) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    // set title
    alertDialogBuilder.setTitle(title);
    // set dialog message
    alertDialogBuilder
        .setMessage(message)
        .setCancelable(false)
        .setPositiveButton(R.string.close_alert, new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, final int id) {
            // if this button is clicked, close the dialog box
            dialog.cancel();
        }
    });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show the message
    alertDialog.show();
    if (progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}

谢谢

4

1 回答 1

2

我们可以在下面的例子中制作进度对话框:

@Override
protected void onPreExecute() {
        loadingDailog = new ProgressDialog(context,AlertDialog.THEME_HOLO_LIGHT);
    ((ProgressDialog) loadingDailog).setIndeterminate(true);
    ((ProgressDialog) loadingDailog).setProgressStyle(ProgressDialog.THEME_HOLO_LIGHT);


    loadingDailog.setMessage("Loading...");
    loadingDailog.show();
    }

当你想消失这个栏时:

 if (loadingDailog != null && loadingDailog.isShowing()) {
        loadingDailog.dismiss();
    }
于 2013-08-28T06:05:11.793 回答