开发者

Android - Cursor exception - Index 1 requested with a size of 1

I have the following code:

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 (!开发者_Go百科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;
}

When I call getVEvents I receive the following exception:

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)

Line 177 corresponds to

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

In both the query method and getVEvents I'm always checking if the cursor is null, and I am using moveToFirst() and moveToNext(), so I'm not quite sure why the exception is occurring, and what exactly "index 1 requested with a size of 1" actually means.

Many thanks!


It means exactly what it says. you're trying to access element 1 (the second since it's zero-based) when the size is only one.

You have a problem with the way you're detecting the end of the data set. Your cursor won't magically become null after you've processed the last row. Instead, it stays the same value but its internal state changes.

You need to detect the end of the data set in a different way.

You could use, for example:

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


The error means that you requested the item with index 1, but the list which your request was sent to only has the size of 1 - meaning it only contains one item and the maximum index therefor is 0.

It would seem that your query only returns a cursor with one row.


Try this code

 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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜