Android - 游标异常 - 请求索引1,大小为1

时间:2011-09-08 12:39:15

标签: android sqlite cursor android-cursor

我有以下代码:

private Cursor query(String selection, String[] selectionArgs,
        String[] columns, String tableName) {
    /*
     * The SQLiteBuilder provides a map for all possible columns requested
     * to actual columns in the database, creating a simple column alias
     * mechanism by which the ContentProvider does not need to know the real
     * column names
     */
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(tableName);
    Cursor cursor = builder.query(mDatabase, columns, selection,
            selectionArgs, null, null, null);
    if (cursor == null) {
        return null;
    } else if (!cursor.moveToFirst()) {
        cursor.close();
        return null;
    }
    return cursor;
}

public List<VEvent> getVEvents(int week, int year) {
    String selection = KEY_WEEK + "=? AND " + KEY_YEAR + "=?";
    String[] selectionArgs = new String[] { String.valueOf(week), String.valueOf(year) };
    Cursor cursor = query(selection, selectionArgs, ALL_CALENDAR_COLUMNS, CALENDAR_TABLE_NAME);
    List<VEvent> events = new ArrayList<VEvent>();
    while (cursor != null) {
        VEvent e = new VEvent();
        try {
        e.getProperties().add(new Uid(cursor.getString(1)));
        e.getProperties().add(new DtStamp(cursor.getString(2)));
        e.getProperties().add(new Organizer(cursor.getString(3)));
        e.getProperties().add(new DtStart(cursor.getString(4)));
        e.getProperties().add(new DtEnd(cursor.getString(5)));
        e.getProperties().add(new Summary(cursor.getString(6)));
        e.getProperties().add(new Location(cursor.getString(7)));
        e.getProperties().add(new Description(cursor.getString(8)));
        events.add(e);
        } catch (ParseException ex) {
            Log.v("getvevents", "parse exception : " + ex.getLocalizedMessage());
        } catch (URISyntaxException ex) {
            Log.v("getvevents", "uri exception : " + ex.getLocalizedMessage());
        }
        cursor.moveToNext();
    }
    return events;
}

当我调用getVEvents时,我收到以下异常:

09-08 11:03:10.121: ERROR/AndroidRuntime(2696):     android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
09-08 11:03:10.121: ERROR/AndroidRuntime(2696):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
09-08 11:03:10.121: ERROR/AndroidRuntime(2696):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
09-08 11:03:10.121: ERROR/AndroidRuntime(2696):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
09-08 11:03:10.121: ERROR/AndroidRuntime(2696):     at com.unibean.SettingsDatabase.getVEvents(SettingsDatabase.java:177)

第177行对应

e.getProperties().add(new Uid(cursor.getString(1)));

在查询方法和getVEvents中我总是检查游标是否为null,并且我正在使用moveToFirst()和moveToNext(),所以我不太确定为什么会发生异常,究竟是什么“索引1请求大小为1“实际上意味着。

非常感谢!

3 个答案:

答案 0 :(得分:2)

这正是它所说的。当大小只有一个时,你正试图访问元素1(第二个,因为它是从零开始的。)

您检测数据集结束时的方式存在问题。处理完最后一行后,cursor不会神奇地变为null。相反,它保持相同的值,但其内部状态发生变化。

您需要以不同的方式检测数据集的结尾。

您可以使用,例如:

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    blah blah blah
    cursor.moveToNext();
}

答案 1 :(得分:1)

错误表示您请求索引为1的项目,但发送请求的列表的大小仅为1 - 这意味着它只包含一个项目,其最大索引为0.

您的查询似乎只返回一行光标。

答案 2 :(得分:0)

试试此代码

 private Cursor query(String selection, String[] selectionArgs,
            String[] columns, String tableName) {
        /*
         * The SQLiteBuilder provides a map for all possible columns requested
         * to actual columns in the database, creating a simple column alias
         * mechanism by which the ContentProvider does not need to know the real
         * column names
         */
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(tableName);
        Cursor cursor = builder.query(mDatabase, columns, selection,
                selectionArgs, null, null, null);
        return cursor;
    }

    public List<VEvent> getVEvents(int week, int year) {
        String selection = KEY_WEEK + "=? AND " + KEY_YEAR + "=?";
        String[] selectionArgs = new String[] { String.valueOf(week), String.valueOf(year) };
        Cursor cursor = query(selection, selectionArgs, ALL_CALENDAR_COLUMNS, CALENDAR_TABLE_NAME);
        List<VEvent> events = new ArrayList<VEvent>();
        while (cursor.moveToNext()) {
            VEvent e = new VEvent();
            try {
            e.getProperties().add(new Uid(cursor.getString(1)));
            e.getProperties().add(new DtStamp(cursor.getString(2)));
            e.getProperties().add(new Organizer(cursor.getString(3)));
            e.getProperties().add(new DtStart(cursor.getString(4)));
            e.getProperties().add(new DtEnd(cursor.getString(5)));
            e.getProperties().add(new Summary(cursor.getString(6)));
            e.getProperties().add(new Location(cursor.getString(7)));
            e.getProperties().add(new Description(cursor.getString(8)));
            events.add(e);
            } catch (ParseException ex) {
                Log.v("getvevents", "parse exception : " + ex.getLocalizedMessage());
            } catch (URISyntaxException ex) {
                Log.v("getvevents", "uri exception : " + ex.getLocalizedMessage());
            }

        }
        return events;
    }