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);
    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() を使用しているため、例外が発生する理由と、正確に "index 1 が要求したもの" が何であるかがよくわかりません。サイズが 1 インチというのは、実際には意味します。

どうもありがとう!

4

3 に答える 3

2

それはまさにそれが言うことを意味します。サイズが 1 しかないときに、要素 1 (ゼロベースなので 2 番目) にアクセスしようとしています。

データセットの終わりを検出する方法に問題があります。最後の行を処理した後、cursor魔法のようにはなりません。null代わりに、同じ値のままですが、内部状態が変化します。

別の方法でデータ セットの終わりを検出する必要があります。

たとえば、次のように使用できます。

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
    blah blah blah
    cursor.moveToNext();
}
于 2011-09-08T12:49:42.877 に答える
1

このエラーは、インデックス 1 のアイテムをリクエストしたが、リクエストが送信されたリストのサイズが 1 しかないことを意味します。つまり、アイテムが 1 つしか含まれておらず、その最大インデックスは 0 です。

クエリは、1 行のカーソルのみを返すようです。

于 2011-09-08T12:49:07.367 に答える
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;
    }
于 2011-09-08T12:49:26.117 に答える