如何获取从SQLite数据库中选择的特定值?当我点击按钮时,app jut崩溃了

时间:2015-03-12 12:42:57

标签: android sqlite

按钮属性。     

按钮btnLogin

的java函数
    public void LoginT(View v){     
        Cursor c = db.rawQuery("select * from Users where username=? and password=?", new String[] {username,password});
         username = c.getString(0);
         password = c.getString(1);

        Toast.makeText(this, (String)username, Toast.LENGTH_LONG).show();

    }

我将使用它进行登录,但我只是测试显示值但没有任何反应

这是logcat

 03-11 06:15:38.008: E/AndroidRuntime(1522): FATAL EXCEPTION: main
03-11 06:15:38.008: E/AndroidRuntime(1522): java.lang.IllegalStateException: Could not execute method of the activity
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.view.View$1.onClick(View.java:3591)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.view.View.performClick(View.java:4084)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.view.View$PerformClick.run(View.java:16966)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.os.Handler.handleCallback(Handler.java:615)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.os.Looper.loop(Looper.java:137)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at java.lang.reflect.Method.invokeNative(Native Method)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at java.lang.reflect.Method.invoke(Method.java:511)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at dalvik.system.NativeStart.main(Native Method)
03-11 06:15:38.008: E/AndroidRuntime(1522): Caused by: java.lang.reflect.InvocationTargetException
03-11 06:15:38.008: E/AndroidRuntime(1522):     at java.lang.reflect.Method.invokeNative(Native Method)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at java.lang.reflect.Method.invoke(Method.java:511)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.view.View$1.onClick(View.java:3586)
03-11 06:15:38.008: E/AndroidRuntime(1522):     ... 11 more
03-11 06:15:38.008: E/AndroidRuntime(1522): Caused by: java.lang.IllegalArgumentException: the bind value at index 2 is null
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
03-11 06:15:38.008: E/AndroidRuntime(1522):     at com.example.twistedfatetraining.Login.LoginT(Login.java:29)
03-11 06:15:38.008: E/AndroidRuntime(1522):     ... 14 more

3 个答案:

答案 0 :(得分:1)

你的代码:

public void LoginT(View v){     
    Cursor c = db.rawQuery("select * from Users where username=? and password=?", new String[] {username,password});
     c.moveToFirst();
     username = c.getString(0);
     password = c.getString(1);

    Toast.makeText(this, (String)username, Toast.LENGTH_LONG).show();

}

答案 1 :(得分:0)

问题似乎是您将空值传递给您的查询:

    Cursor c = db.rawQuery("select * from Users where username=? and password=?", new String[] {username,password});

您正在传递用户名和密码,但此时似乎尚未初始化。我想你会尝试做别的事情。这有点像这样:

    Cursor c = db.rawQuery("select * from Users where username=? and password=?", new String[] {"David","hello"});

要使您的查询工作,用户名和密码必须初始化且有效...

答案 2 :(得分:0)

  

java.lang.IllegalArgumentException:索引2处的绑定值为null

问题在于查询中的选择参数。密码可能为空

在运行查询之前尝试检查输入值:

if (username == null || password == null) {
   // show dialog or toast
   return 
}
Cursor c = db.rawQuery("select * from Users where username=? and password=?", new String[] {username, password});