【问题标题】:Android java.lang.IllegalStateException: Couldn't init cursor windowAndroid java.lang.IllegalStateException:无法初始化光标窗口
【发布时间】:2012-10-18 19:32:47
【问题描述】:

我在 google play 崩溃报告中有这个错误,不知道如何修复它:

java.lang.IllegalStateException: Couldn't init cursor window
at android.database.CursorWindow.native_init(Native Method)
at android.database.CursorWindow.<init>(CursorWindow.java:41)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:276)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:268)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:171)
at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)
at com.jim2.UpdateService.restaureNotifications(UpdateService.java:274)
at com.jim2.helpers.Globals.updateWidgets(Globals.java:1011)
at com.jim2.helpers.Globals.onReceive(Globals.java:746)
at com.jim2.UpdateService$5.onSignalStrengthsChanged(UpdateService.java:747)
at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:387)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3693)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)

这是我崩溃的代码:

    DataBaseHelper d = new DataBaseHelper(context);
    Cursor c = d.getNotifications(false);
    SharedPreferences preferences;
    String ns = Context.NOTIFICATION_SERVICE;

    NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);

    CharSequence tickerText = "";

    if(c!=null && c.moveToFirst()){ // CRASH HERE :(

        do{
                       ....
       while(c.moveToNext());
       if(c!=null && !c.isClosed()) {
                c.close();c = null;
       }
    }
    d.close();

这里是从 getNotifications 获取光标的代码:

public Cursor getNotifications(boolean all) {
        //removeAllNotifications();
        Cursor c;
        String fields[] = new String[] { "id", "ordre", "is_active"};
        String where = "is_active = 1";
        if(all)where = "";
        c = myDataBase.query(TABLE, fields, where, null, null, null, "ordre");

        return c;
    }

我的问题是我无法重现崩溃,因此很难修复原因,我的设备都可以正常工作。

已经看到这篇帖子Android: Couldn't init Cursor Window,但我在 moveToFirst 上崩溃了,我已经在答案中添加了代码。

非常感谢

【问题讨论】:

    标签: android database cursor android-sqlite illegalstateexception


    【解决方案1】:

    本机代码抛出此异常。 (参考this

    主要是由于未能为正在创建的新游标分配内存。所以我的猜测是您获取的数据有时会太多(内存中几乎超过 1M),或者您打开大量游标而不关闭它们,直到您用完内存来分配新缓冲区。

    1. 检查您的代码并确保在 finally 子句中关闭所有光标。
    2. 如果从数据库中获取的数据可能太大,请将它们分成单独的块(例如拉取数据以获取更多内容)。

    如果这有帮助,请告诉我。

    【讨论】:

    • 非常感谢!我无法测试,因为我无法重现错误,但我检查了我的代码并忘记了一些 close() 所以我认为是这个(我希望)。这不可能是我的数据,因为我最多有 5 行和 3 个字段 ^^
    • 您好,我已经在 google play 上进行了更新,但异常仍然存在 :( 我进行了搜索,现在我的所有光标都已关闭。您还有其他想法来解决这个问题吗?
    • 我很确定悬空的未闭合游标是此错误的主要原因和常见原因。每个打开的游标都会分配 1M 的内存,直到没有空间可做。请让其他人(不同的眼睛)查看您的代码。请告诉我你发现了什么。
    • 好的,我会再看一遍,并尝试将我的代码展示给其他人以确保。现在我只有一个最新版本的报告,所以可能是以前版本的非关闭光标造成了这个崩溃......有可能吗?
    • 我总是崩溃:(但我认为这是一个内存问题,就像你说的那样。但是这个内存问题不再是由非关闭游标引起的。我不知道如何找到内存问题:(
    【解决方案2】:

    不确定,但不应该是这样

            c = myDataBase.query(TABLE, fields, where, null, null, null, "ordre DESC")
    

    【讨论】:

    • 没有原因 ordre 是一个字段,它不是 ORDER,我想要 0 到 x,所以它是 ASC(或者没有,因为它是默认值)
    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多