布尔值在我的异步任务中不起作用

时间:2012-12-03 12:45:52

标签: android asynchronous background task

我遇到了问题。我的异步任务做得很好。但是在我的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 个答案:

答案 0 :(得分:3)

你应该使用双等号。

if (error == true) {   

答案 1 :(得分:2)

用于Java中的相等性测试的运算符是===是赋值运算符。现在,error始终为true,因为您每次都会为true分配{{1}}。希望这会有所帮助。

答案 2 :(得分:1)

要进行比较,您应该使用error == true而不是error = true这是一项任务,并且始终为真。

答案 3 :(得分:1)

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