android - CursorIndexOutOfBoundsException 与 cursor.getInt()

标签 android sqlite cursor

这是我的代码:

 Log.d("MYTAG", "random number -->" + randomNumber);
 idRandomCards[i] = cursorTypeZero.getInt(randomNumber);

这是错误日志:

7620-7620/package D/MYTAG﹕ random number -->94
04-02 16:06:51.804    7620-7620/package D/AndroidRuntime﹕ Shutting down VM
04-02 16:06:51.804    7620-7620/package W/dalvikvm﹕ threadid=1: thread exiting    with uncaught exception (group=0x41bafda0)
04-02 16:06:51.854    7620-7620/package D/dalvikvm﹕ GC_FOR_ALLOC freed 1283K,     45% free 9237K/16608K, paused 42ms, total 46ms
04-02 16:06:51.864    7620-7620/package E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: package, PID: 7620
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 206
        at ecc

如您所见,本例中的随机数值为 94。cursor.getCount() = 206。

为什么我有这个异常?

我不想使用 cursor.getColumnIndexOrThrow() 方法,因为我需要一个随机行。

有人可以帮助我吗?

最佳答案

尝试使用

 cursorTypeZero.moveToPosition(position) 

在 getInt() 方法之前。 在@blackbelt 回答后编辑..

关于android - CursorIndexOutOfBoundsException 与 cursor.getInt(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29415353/

相关文章:

sqlite - 在 SQLite 中查询

ios - 尝试访问 ios 时,sqlite 预填充数据库为空

Android 应用程序在 SQLite 数据库插入时卡住

android - 当 API 关闭时改造 SocketTimeoutException 错误

android - android 是否总是使用最新的 API 来运行应用程序?

android - 需要解释 : getText() throws error on getActitvity()ut not on view

android - 在 textview 中获取当前可见文本

ios - Sqlite 与文档目录

php - Symfony2/Doctrine2 Oracle 游标问题

mysql - MYSQL 中的动态游标