我想在 listview 中显示 SQLite 数据库中所有列的平均值。我在查询中使用平均函数和 group by 子句,但是当我运行应用程序时,我的应用程序崩溃并收到 IllegalArgumentException 并且绑定(bind)值索引 1 为 null。
我不知道我的错误在哪里,即使我没有在 SQLite db 中插入任何空值。SO 可以帮助我。提前致谢。
这是我的代码:
private void showPerformanceDetails()
{
ArrayList<Performance_Pojo> Performance_PojoList = new ArrayList<Performance_Pojo>();
Performance_PojoList.clear();
SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();
Cursor cursor = sqlDatabase.rawQuery("SELECT performance_month, AVG(performance_rate_one), AVG(performance_rate_two), AVG(performance_rate_three), AVG(performance_rate_four), AVG(performance_rate_five) FROM performance where "+ "Emp_id" + " = ? "
+" GROUP BY performance_month",new String[]{strSeparated_Id});
if (cursor != null && cursor.getCount() != 0)
{
if (cursor.moveToFirst())
{
do
{
Performance_Pojo Performance_PojoListItems = new Performance_Pojo();
Performance_PojoListItems.set_strPerformanceMonth(cursor.getString(cursor.getColumnIndex("performance_month")));
Performance_PojoListItems.set_strPerformance_rate_one(cursor.getString(cursor.getColumnIndex("performance_rate_one")));
Performance_PojoListItems.set_strPerformance_rate_two(cursor.getString(cursor.getColumnIndex("performance_rate_two")));
Performance_PojoListItems.set_strPerformance_rate_three(cursor.getString(cursor.getColumnIndex("performance_rate_three")));
Performance_PojoListItems.set_strPerformance_rate_four(cursor.getString(cursor.getColumnIndex("performance_rate_four")));
Performance_PojoListItems.set_strPerformance_rate_five(cursor.getString(cursor.getColumnIndex("performance_rate_five")));
Performance_PojoList.add(Performance_PojoListItems);
}while (cursor.moveToNext());
}
db.close();
cursor.close();
}
PerformanceList_Adapter performanceList_Adapter = new PerformanceList_Adapter(Performance_Details.this, Performance_PojoList);
list_PerformanceDetails.setAdapter(performanceList_Adapter);
}
这是我的日志猫错误:
07-01 12:15:15.366: E/AndroidRuntime(14850): FATAL EXCEPTION: main
07-01 12:15:15.366: E/AndroidRuntime(14850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sqlitedemo/com.sqlitedemo.Performance_Details}: java.lang.IllegalArgumentException: the bind value at index 1 is null
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.os.Looper.loop(Looper.java:123)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 12:15:15.366: E/AndroidRuntime(14850): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 12:15:15.366: E/AndroidRuntime(14850): at java.lang.reflect.Method.invoke(Method.java:507)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 12:15:15.366: E/AndroidRuntime(14850): at dalvik.system.NativeStart.main(Native Method)
07-01 12:15:15.366: E/AndroidRuntime(14850): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:237)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:185)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.sqlitedemo.Performance_Details.showPerformanceDetails(Performance_Details.java:79)
07-01 12:15:15.366: E/AndroidRuntime(14850): at com.sqlitedemo.Performance_Details.onCreate(Performance_Details.java:63)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 12:15:15.366: E/AndroidRuntime(14850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
最佳答案
“绑定(bind)值”显然是指您在 rawQuery() 中选择的选择参数。如果这样的 selArgs 值为空,您会得到这个。
编辑:
Cursor cursor = sqlDatabase.rawQuery("SELECT performance_month, AVG(performance_rate_one), AVG(performance_rate_two), AVG(performance_rate_three), AVG(performance_rate_four), AVG(performance_rate_five) FROM performance where "+ "Emp_id" + " = ? "
+" GROUP BY performance_month",new String[]{strSeparated_Id});
所有参数位置都应该用 ? 指定。尽管如此,查询的参数始终是字符串,因此整数没有什么特别的。这应该适合你:
更改:
new String[]{strSeparated_Id}
至
new String[]{String.valueOf(strSeparated_Id)}
关于java - IllegalArgumentException Android 中的 SQLite 数据库绑定(bind)值索引 1 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24504213/