0

我有问题。我的异步任务做得很好。但是在我的 OnPostExecute 我得到了这个:

if (error = true) {
   loginErrorMsg.setText("Incorrect username/password");
} else {
   loginErrorMsg.setText("");
}

即使error == false他仍然显示:不正确的用户名/密码......

class LoginUser extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Loading");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        email = inputEmail.getText().toString();
        password = inputPassword.getText().toString();

    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {

        UserFunctions userFunction = new UserFunctions();
        Log.d("Button", "Login");
        JSONObject json = userFunction.loginUser(email, password);
     // check for login response
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                String res = json.getString(KEY_SUCCESS); 
                if(Integer.parseInt(res) == 1){
                    // user successfully logged in
                    // Store user details in SQLite Database
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Clear all previous data in database
                    userFunction.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                    // Launch Dashboard Screen
                    Intent dashboard = new Intent(getApplicationContext(), Main.class);

                    // Close all views before launching Dashboard
                    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(dashboard);

                    // Close Login Screen
                    finish();
                }else{
                    // Error in login
                    error = true;
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();


        if( error = true){
            loginErrorMsg.setText("Incorrect username/password");
        } else {
            loginErrorMsg.setText("");
        }
    }

}
4

4 回答 4

3

您应该使用双等号。

if (error == true) {   
于 2012-12-03T12:48:01.633 回答
2

Java 中用于相等性测试的运算符 is ==,=是赋值运算符。现在你error将永远是因为你每次true都分配给它。true希望这可以帮助。

于 2012-12-03T12:49:07.600 回答
1

要进行比较,您应该使用error == truenoterror = true哪个是分配并且始终为真。

于 2012-12-03T12:48:24.990 回答
1

更改if(error = true)if(error == true)

于 2012-12-03T12:48:26.687 回答