CurSorIndexOutOfBoundSexception:请求索引2,大小为2。如何解决?
查询SQLite数据库时,仅显示2行。是否可以解决此错误?
Cursor cursor = database.rawQuery("SELECT * FROM Category", null);
cursor.moveToFirst();
for (int i = 0; i < 10; i++) {
categoryList.add(new Category(cursor.getInt(0), cursor.getString(1)));
cursor.moveToNext();
}
cursor.close();
database.close();
错误:
Process: com.triangle.learningenglish, PID: 30477
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.triangle.learningenglish/com.triangle.learningenglish.Select.SelectCategory}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
When querying the SQLite database, only 2 lines are displayed. Is it possible to fix this error?
Cursor cursor = database.rawQuery("SELECT * FROM Category", null);
cursor.moveToFirst();
for (int i = 0; i < 10; i++) {
categoryList.add(new Category(cursor.getInt(0), cursor.getString(1)));
cursor.moveToNext();
}
cursor.close();
database.close();
Error:
Process: com.triangle.learningenglish, PID: 30477
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.triangle.learningenglish/com.triangle.learningenglish.Select.SelectCategory}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误的原因是您没有检查光标是empity。您只需循环循环循环并检索可能具有或没有价值的结果。
我想这条代码解决了您的问题
the reason for that error is you are not checking that if a cursor is empity. you just simply cycle over the for loop and retrieving the result which may have or have not a value.
i guess this line of code solves your problem