我在允许类访问我的数据库的方法上收到错误消息。错误发生在 .getReadableDatabase()。收到的错误如下。不知道如何解决这个错误。
Cannot make a static reference to the non-static method getReadableDatabase() from the type SQLiteOpenHelper
方法:
public static boolean vaidateUser(String username, String password) {
Cursor c = getReadableDatabase().rawQuery(
"SELECT * FROM " + TABLE_NAME_CREDENTIALS + " WHERE "
+ COLUMN_NAME_USERNAME + "='" + username +"'AND "+COLUMN_NAME_PASSWORD+"='"+password+"'" , null);
if (c.getCount()>0)
return true;
return false;
用于调用此方法:
lsLogin.setOnClickListener (new OnClickListener() {
@Override
public void onClick(View v) {
//check login
String username = lsUsername.getText().toString();
String password = lsPassword.getText().toString();
try{
if(LoginDB.vaidateUser(username,password)) {
Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
startActivity(goToNextActivity);
Toast.makeText(LoginScrExample.this,"Login Sucessful",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LoginScrExample.this,"Invalid Username/Password",Toast.LENGTH_LONG).show();
}
}