【问题标题】:Android - Cursor exception - Index 1 requested with a size of 1Android - 光标异常 - 请求的索引 1 大小为 1
【发布时间】:2022-03-17 23:06:18
【问题描述】:

我有以下代码:

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 中,我总是检查光标是否为空,并且我正在使用 moveToFirst() 和 moveToNext(),所以我不太确定为什么会发生异常,以及究竟是什么“请求大小为 1" 的索引 1 实际上意味着。

非常感谢!

【问题讨论】:

  • 试试 moveToFirst() 和 getString(0)。

标签: android sqlite android-cursor


【解决方案1】:

这正是它所说的意思。当大小仅为 1 时,您正在尝试访问元素 1(第二个,因为它是从零开始的)。

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

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

你可以使用,例如:

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

【讨论】:

  • 你的意思是 while(!cursor.isAfterLast()) 吗?
  • @Jonny,是的,你一定是在编辑过程中发现了我,我刚刚注意到了那个错误。
【解决方案2】:

该错误表示您请求了索引为 1 的项目,但您的请求被发送到的列表的大小仅为 1 - 这意味着它仅包含一项并且其最大索引为 0。

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

【讨论】:

    【解决方案3】:

    试试这个代码

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

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 2022-01-12
      • 2018-11-04
      相关资源
      最近更新 更多