0

向那些有带宽问题的人道歉。

所以,我在一个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。我很抱歉浪费了大家的时间。

4

2 回答 2

3

做这个:

if(c.moveToFirst()){
    int boolAsInt = c.getInt(1);
}

光标可能为空,因此您必须在尝试从中获取任何值之前检查它。如果光标为空,则 moveToFirst() 返回 false,因此不会输入 if 并且您不会再收到异常

空游标的原因是 SQL 查询返回了一个空的结果集。如果您确定您应该得到一个非空集,请再次检查查询。

于 2012-07-12T22:19:25.337 回答
0

getInt 方法的参数是索引,所以如果你想获取查询的第一列,你应该输入 0。

于 2014-12-08T07:23:37.267 回答