java - android.database.CursorIndexOutOfBoundsException : No idea why

标签 java android database sqlite

我刚刚收到使用我的 Android 应用程序的用户的错误报告。

java.lang.RuntimeException: Unable to start activity ComponentInfo{c`om.gurpswu.gurps/com.gurpswu.gurps.Home}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
at android.app.ActivityThread.access$2300(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
at com.gurpswu.gurps.Player.getIntField(Player.java:137)
at com.gurpswu.gurps.Home.hudSetup(Home.java:102)
at com.gurpswu.gurps.Home.onCreate(Home.java:25)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
... 11 more`

我无法重现该问题。我希望你可以帮助我。这是我在主屏幕上从 sqlite 数据库检索值的代码。

 public String getStringField(String fieldName) {
        String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_CITY,
                KEY_HEALTH, KEY_ENERGY, KEY_RANK, KEY_CASH, KEY_BALANCE,
                KEY_DONATED };

        Cursor c = ourDatabase.query(true, DATABASE_TABLE, columns, null, null,
                null, null, "_id DESC", "1");

        if (c != null) {
            c.moveToFirst();
            String name = c.getString(c.getColumnIndexOrThrow(fieldName));
            c.close();
            return name;
        }
        return null;
    }

    public int getIntField(String fieldName) {
        String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_CITY,
                KEY_HEALTH, KEY_ENERGY, KEY_RANK, KEY_CASH, KEY_BALANCE,
                KEY_DONATED };

        Cursor c = ourDatabase.query(true, DATABASE_TABLE, columns, null, null,
                null, null, "_id DESC", "1");

        if (c != null) {
            c.moveToFirst();
            int result = c.getInt(c.getColumnIndexOrThrow(fieldName));
            c.close();
            return result;
        }
        return (Integer) null;
    }

最佳答案

在堆栈跟踪中,第一行有 CursorIndexOutOfBoundsException: Index 0 requests, with a size of 0。请在 c.moveToFirst() 之前检查 c.getCount():

if(c != null) {
    try {
        if (c.getCount() > 0) {
            c.moveToFirst();
            return c.getInt(c.getColumnIndexOrThrow(fieldName));
        }
    }
    finally {
        c.close();
    }
}

关于java - android.database.CursorIndexOutOfBoundsException : No idea why,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11370916/

相关文章:

java - 抛出多个自定义异常时如何处理

android - Kotlin Coroutine,Android Async Task 和 Async await 的区别

JavaScript 或 React - 单页网站中的数据库安全

php - 如何在多个表上设置一个FK(外键)?

java - 我可以在 OrientDB 上查询期间访问对象的集群名称吗?

java - 如何通过命令行启动spring-boot应用程序?

java - 如何设计相互依赖的可扩展类型基础设施

android - 如何将 Facebook 应用所有权迁移到商务管理平台?

java - 为什么我的 Android 线程这么慢?

sql - 寻找最高值(value)