Android java.lang.IllegalStateException : Couldn't init cursor window

标签 android database cursor android-sqlite illegalstateexception

我在 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 上崩溃而不是稍后,并且我已经在答案中添加了代码。

非常感谢

最佳答案

此异常是由 native 代码引发的。 (引用this)

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

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

请告诉我这是否有帮助。

关于Android java.lang.IllegalStateException : Couldn't init cursor window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12928708/

相关文章:

java - android加载库失败

java - 修复 Android Studio/Kotlin 中单击按钮时出现的错误?

sql - 加入不存在的表

mysql - 通过不断的编辑维护订单数据的完整性

c++ - LoadCursorFromFile 返回 NULL。 GetLastError 也为 0

python - 如何更改 mySQL 查询结果中的一行?

android - Opengl 创建 FBO- 致命信号 11 错误

ruby-on-rails - 几种不同通知事件的数据库设计

python - 游标在 PyMySQL 中起什么作用和作用?

android - 自定义 Android 电话应用程序