错误的密码/电子邮件错误身份验证活动-使用SQLite

时间:2018-07-12 06:19:00

标签: database sqlite authentication android-sqlite

Onclick()方法有什么问题?还是verifyFromSqlite()?尝试使用刚刚提供给注册表格的数据登录,为什么按登录按钮的输出只是密码/电子邮件错误的错误?

 public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

private final AppCompatActivity activity = LoginActivity.this;

private NestedScrollView nestedScrollView;

private TextInputLayout textInputLayoutEmail;
private TextInputLayout textInputLayoutPassword;

private TextInputEditText textInputEditTextEmail;
private TextInputEditText textInputEditTextPassword;

private AppCompatButton appCompatButtonLogin;

private AppCompatTextView textViewLinkRegister;

private InputValidation inputValidation;
private DatabaseHelper databaseHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getSupportActionBar().hide();


    initViews();
    initListeners();
    initObjects();


}
private void initViews(){
    nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);

    textInputLayoutEmail= (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
    textInputLayoutPassword= (TextInputLayout) findViewById(R.id.textInputLayoutPassword);

    textInputEditTextEmail=(TextInputEditText) findViewById(R.id.textInputEditTextEmail);
    textInputEditTextPassword=(TextInputEditText) findViewById(R.id.textInputEditTextPassword);

    appCompatButtonLogin = (AppCompatButton) findViewById(R.id.appCompatButtonLogin);
    textViewLinkRegister= (AppCompatTextView) findViewById(R.id.textViewLinkRegister);
}

private void initListeners(){
    appCompatButtonLogin.setOnClickListener(this);
    textViewLinkRegister.setOnClickListener(this);
}

private void initObjects(){
    databaseHelper = new DatabaseHelper(activity);
    inputValidation = new InputValidation(activity);
}

@Override
public void onClick(View v){
    switch (v.getId()){
        case R.id.appCompatButtonLogin:
            verifyFromSQLite();
            break;

        case R.id.textViewLinkRegister:
            Intent intentRegister = new Intent(getApplicationContext(), RegisterActivity.class);
            startActivity(intentRegister);
            break;
    }
}

private void verifyFromSQLite(){
    if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))){
        return;

    }
    if (!inputValidation.isInputEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))){
        return;
    }

    if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, getString(R.string.error_message_password))){
        return;
    }

    if(databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim()
            , textInputEditTextPassword.getText().toString().trim())){
        Intent accountsIntent = new Intent(activity, UsersActivity.class);
        accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
        emptyInputEditText();
        startActivity(accountsIntent);
    } else {
        Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
    }
}

private void emptyInputEditText(){
    textInputEditTextEmail.setText(null);
    textInputEditTextPassword.setText(null);
}
}

这是我做错的事。

 if(databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim()
            , textInputEditTextPassword.getText().toString().trim())){
        Intent accountsIntent = new Intent(activity, UsersActivity.class);
        accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
        emptyInputEditText();
        startActivity(accountsIntent);
    } else {
        Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
    }
}

请让我知道我在做什么错,因为我尝试了所有其他答案,但它不会单击给我:)为什么注册并尝试登录后出现错误消息?

1 个答案:

答案 0 :(得分:0)

假设 checkUser 方法与您之前的question相同,即:-

public boolean checkUser(String password, String email){
    String[] columns = {
            COLUMN_USER_ID
    };
    SQLiteDatabase db= this.getWritableDatabase();
    String selection = COLUMN_USER_EMAIL + " = ? " + "AND "+ COLUMN_USER_PASSWORD+" =? ";
    String[] selectionArgs = { email,password };

    Cursor cursor = db.query(TABLE_USER,
            columns,
            selection,
            selectionArgs,
            null,
            null,
            null);
    int cursorCount = cursor.getCount();
    cursor.close();
    db.close();

    if(cursorCount > 0){
        return true;
    }
    return false;
 }

然后,您将电子邮件作为密码传递,并将密码作为电子邮件传递。尝试更改:-

if(databaseHelper.checkUser(textInputEditTextEmail.getText().toString().trim()
            , textInputEditTextPassword.getText().toString().trim())){
        Intent accountsIntent = new Intent(activity, UsersActivity.class);
        accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
        emptyInputEditText();
        startActivity(accountsIntent);
    } else {
        Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
    }
}

至:-

if(databaseHelper.checkUser( textInputEditTextPassword.getText().toString().trim()
            ,textInputEditTextEmail.getText().toString().trim())){
        Intent accountsIntent = new Intent(activity, UsersActivity.class);
        accountsIntent.putExtra("EMAIL", textInputEditTextEmail.getText().toString().trim());
        emptyInputEditText();
        startActivity(accountsIntent);
    } else {
        Snackbar.make(nestedScrollView, getString(R.string.error_valid_email_password), Snackbar.LENGTH_LONG).show();
    }
}