使用游标的BindValue错误

时间:2018-03-06 14:37:11

标签: android sqlite android-intent android-sqlite

我想将数据与意图一起发送到另一个活动以便在光标中使用但是我的方式不工作

主要活动:

int classId = getIntent().getIntExtra("classId" , 0);

             List<StudentTable> tableData = new ArrayList<>();

             Cursor cursor = database.rawQuery("select * from " + tblName_Student + " where class_id = ?",
                  new String[classId]);
             close();

             while (cursor.moveToNext())
             {
             StudentTable table = new StudentTable();

             table.setStudentName(cursor.getString(2));

             tableData.add(table);
             }

目标活动:

03-06 17:41:53.274 11676-11676/com.example.user.classmanager     E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.classmanager, PID: 11676
java.lang.RuntimeException: Unable to start activityComponentInfo{com.example.user.classmanager/com.example.user.classmanager.StudentList}: java.lang.IllegalArgumentException: the bind value at index 2 is null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: the bind value at index 2 is null

LogCat错误:

{{1}}

1 个答案:

答案 0 :(得分:1)

我相信您的问题是使用以下代码: -

         Cursor cursor = database.rawQuery("select * from " + tblName_Student + " where class_id = ?",
              new String[classId]);
         close();

这是传递/使用未初始化的数组,其中元素的数量等于classId的值。所有元素都为null,因为没有使用任何值初始化。

相反,您应该传递一个包含单个元素的数组,并使用classId的值进行初始化。以下应该这样做: -

         Cursor cursor = database.rawQuery("select * from " + tblName_Student + " where class_id = ?",
              new String[]{String.valueOf(classId)}); //<<<<< declare and initialise a String array with 1 element.
         close();