目前我正在使用Android Studio(Java)开发移动应用程序。我想创建一个包含用户帐户信息的SQLite数据库。要登录,需要验证用户凭据(用户名+密码)。为此,我想将EditText中的字符串与存储在数据库中的凭据进行比较。当用户触摸/按下按钮时,调用方法'check'。在下面,你可以看到它是如何工作的(在MainActivity中):
public class MainActivity extends AppCompatActivity {
TextView TV1,TV2;
EditText et_username, et_password;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV1 = (TextView) findViewById(R.id.TV1);
TV1.setText("Please fill in your username and password.");
TV1.setTextColor(Color.BLUE);
TV2 = (TextView) findViewById(R.id.TV2);
TV2.setTextColor(Color.RED);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Account(" +
"account_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"username TEXT NOT NULL," +
"password TEXT NOT NULL);");
db.execSQL("INSERT INTO Account (username,password) VALUES ('Simon','123');");
db.execSQL("INSERT INTO Account (username,password) VALUES ('Bart','abc');");
db.execSQL("INSERT INTO Account (username,password) VALUES ('Sytze','lol');");
}
public void check(View view){
String username = et_username.getText().toString();
String password = et_password.getText().toString();
Cursor resultSet = db.rawQuery("SELECT username,password FROM Account WHERE username='" + username + "';", null);
resultSet.moveToFirst();
String gbTest = resultSet.getString(resultSet.getColumnIndex("username"));
String wwTest = resultSet.getString(resultSet.getColumnIndex("password"));
resultSet.close();
String res = gbTest + " : " + wwTest;
TV2.setText(res);
/*
if (password.toString() != wwTest){
TV2.setText("Wrong combination...");
}
*/
}
}
当我按下按钮时出现错误(即应用程序崩溃)。但是,当我将resultSet.rawQuery中的'username'替换为数据库中的实际用户名时,比如'Bart',它确实有效。显然,EditText的字符串与实际字符串之间存在差异。这是怎么来的,为什么我不能使用EditText的字符串?我希望我的问题和疑问是明确的,但如果没有:请告诉我。
提前感谢您的帮助。
电贺, Sytze
EDIT 这是我得到的错误:
06-21 15:29:34.194 2114-2114/sytzesimonse.databaseapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17749)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5473)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17749)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5473)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:427)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at sytzesimonse.databaseapp.MainActivity.check(MainActivity.java:56)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17749)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5473)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)