CursorIndexOutOfBoundsException:请求索引0,大小为0.错误 - Android

时间:2012-01-05 18:31:40

标签: android sqlite exception expandablelistview

我正在尝试在expandableListView中获取子项的位置id,并根据点击的子项启动另一个类,我这样做:

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) 
    {
        // TODO Auto-generated method stub
        super.onChildClick(parent, v, groupPosition, childPosition, id);
        Intent i = new Intent(this, SavedEntries.class);
        i.putExtra("_id", id);
        startActivityForResult(i, ACTIVITY_EDIT);
        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }

但我一直收到这个错误:

01-05 13:14:28.232: E/AndroidRuntime(316): FATAL EXCEPTION: main
01-05 13:14:28.232: E/AndroidRuntime(316): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x17.projects.strikezone/com.x17.projects.strikezone.SavedEntries}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.os.Looper.loop(Looper.java:123)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-05 13:14:28.232: E/AndroidRuntime(316):  at java.lang.reflect.Method.invokeNative(Native Method)
01-05 13:14:28.232: E/AndroidRuntime(316):  at java.lang.reflect.Method.invoke(Method.java:521)
01-05 13:14:28.232: E/AndroidRuntime(316):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-05 13:14:28.232: E/AndroidRuntime(316):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-05 13:14:28.232: E/AndroidRuntime(316):  at dalvik.system.NativeStart.main(Native Method)
01-05 13:14:28.232: E/AndroidRuntime(316): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-05 13:14:28.232: E/AndroidRuntime(316):  at com.x17.projects.strikezone.SavedEntries.getDateRow(SavedEntries.java:56)
01-05 13:14:28.232: E/AndroidRuntime(316):  at com.x17.projects.strikezone.SavedEntries.onCreate(SavedEntries.java:48)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 13:14:28.232: E/AndroidRuntime(316):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-05 13:14:28.232: E/AndroidRuntime(316):  ... 11 more

这是我发送id的地方:

Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong("_id") : null;

    private void getDateRow()
    {
        Cursor mCursor = mDbHelper.fetchRow(mRowId);
        date = mCursor.getString(0); //Line 56, this is where the error occurs.
        mCursor.close();
    }

这是上面的查询:

public Cursor fetchRow(long rowId)
    {
        Cursor cursor = mDb.rawQuery("SELECT date FROM entry WHERE _id=" + rowId, null);
        if(cursor != null)
        {
            cursor.moveToFirst();
        }
        return cursor;
    }

为什么我收到此错误?我在这里做错了什么?

提前致谢。

3 个答案:

答案 0 :(得分:2)

光标位于空位置。循环光标以获取行。

while(mCursor.moveToNext()) {
    date = mCursor.getString(0);
}

答案 1 :(得分:1)

这是因为你没有在Cursor中获得任何行。确保fetchRow(...)确实返回了一些数据。是字符串类型的日期字段吗?

答案 2 :(得分:0)

在getDateRow方法中,在从中获取任何内容之前调用mCursor.moveToFirst()方法。如果你将它作为条件使用它是最好的,因为mCursor.moveToFirst()返回一个布尔值,如果光标可以移动到第一个则为true,如果光标不能移动则为false。 这就是我的意思。

   private void getDateRow(){
        Cursor mCursor = mDbHelper.fetchRow(mRowId);
        if (mCursor.moveToFirst()){
        date = mCursor.getString(0);
        }
        mCursor.close();
    }