Android - 游标异常 - 请求索引 1,大小为 1
我有以下代码:
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”到底意味着什么。
非常感谢!
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 (!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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它的意思正是它所说的。当大小仅为一时,您尝试访问元素 1(第二个元素,因为它是从零开始的)。
您检测数据集结尾的方式有问题。处理完最后一行后,您的
光标
不会神奇地变成null
。相反,它保持相同的值,但其内部状态发生变化。您需要以不同的方式检测数据集的结尾。
例如,您可以使用:
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 becomenull
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:
该错误意味着您请求了索引为 1 的项目,但您的请求发送到的列表的大小仅为 1 - 这意味着它只包含一项,其最大索引为 0。
您的查询似乎只返回一行光标。
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