所以,我在SQLiteDatabaseHelper
类中有一个方法,它读取一个数字并返回一个布尔值,如上图所示。但是,执行代码只会抛出此异常:
06-28 19:49:45.881: E/AndroidRuntime(355): FATAL EXCEPTION: main
06-28 19:49:45.881: E/AndroidRuntime(355): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-28 19:49:45.881: E/AndroidRuntime(355): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.mowdownDevelopments.nagTasks.NagTasksDatabaseHelper.isTaskChecked(NagTasksDatabaseHelper.java:239)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.mowdownDevelopments.nagTasks.NagTaskListFragment.onPrepareActionMode(NagTaskListFragment.java:184)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.ActionBarSherlockCompat$ActionModeCallbackWrapper.onPrepareActionMode(ActionBarSherlockCompat.java:1181)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.app.ActionBarImpl$ActionModeImpl.invalidate(ActionBarImpl.java:713)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.app.ActionBarImpl.startActionMode(ActionBarImpl.java:405)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.internal.ActionBarSherlockCompat.startActionMode(ActionBarSherlockCompat.java:178)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.actionbarsherlock.app.SherlockFragmentActivity.startActionMode(SherlockFragmentActivity.java:50)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.mowdownDevelopments.nagTasks.NagTaskListFragment.onListItemClick(NagTaskListFragment.java:62)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.widget.ListView.performItemClick(ListView.java:3382)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.os.Handler.handleCallback(Handler.java:587)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.os.Looper.loop(Looper.java:123)
06-28 19:49:45.881: E/AndroidRuntime(355): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 19:49:45.881: E/AndroidRuntime(355): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 19:49:45.881: E/AndroidRuntime(355): at java.lang.reflect.Method.invoke(Method.java:521)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 19:49:45.881: E/AndroidRuntime(355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 19:49:45.881: E/AndroidRuntime(355): at dalvik.system.NativeStart.main(Native Method)
这指出了问题所在:
public boolean isTaskChecked(long taskID){
SQLiteDatabase db = getReadableDatabase();
String[] columns = {ID, CHECKED};
Cursor c = db.query(TASKS, columns, "_id="+taskID, null, null, null, null);
c.moveToFirst();
int boolAsInt = c.getInt(1); //This line is throwing.
//db.close();
c.close();
if (boolAsInt==1) {
return true;
} else if (boolAsInt==0) {
return false;
} else {
panicButton();
}
return false;
}
如图所示,通过ADB执行查询就可以了。 (我在那里使用了通用查询,但是select _id, ischecked from tasks where _id=1;
也工作得很好。)我不明白为什么当代码明确要求列索引1时,我正在请求索引0,以及大小如何在shell中执行查询时,0给我一些有效的东西(并且不是基于零的大小?)。任何人都可以帮我调试吗?
[编辑:] 所以,事实证明问题是,对于这个特殊问题,我总是从另一个班级传递-1。我为浪费每个人的时间而道歉。
答案 0 :(得分:3)
这样做:
if(c.moveToFirst()){
int boolAsInt = c.getInt(1);
}
光标可能为空,因此您必须在尝试从中获取任何值之前进行检查。 如果光标为空,则moveToFirst()返回false,因此不会输入if并且您将不再获得异常
空游标的原因是SQL查询返回空结果集。如果您确定应该获得非空集,请再次检查查询。
答案 1 :(得分:0)
getInt方法的参数是索引,因此如果您尝试获取查询的第一列,则应该输入0。