所以,我在一个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,以及执行查询时大小如何为 0在 shell 中给了我一些有用的东西(而且大小不是从零开始的吗?)。谁能帮我调试一下?
[编辑:]所以,事实证明,对于这个特定问题,问题在于我总是从不同的班级传递-1。我很抱歉浪费了大家的时间。