Android java.lang.IllegalStateException:无法初始化游标窗口

jau*_*ard 2 database android cursor illegalstateexception android-sqlite

我在谷歌播放崩溃报告上有这个错误,不知道如何解决它:

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)
Run Code Online (Sandbox Code Playgroud)

这是我的代码谁崩溃:

    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();
Run Code Online (Sandbox Code Playgroud)

这里是从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;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是我无法重现崩溃所以很难修复我的设备上的原因所有工作正常.

已经看到这篇文章Android:无法初始化Cursor Window但是我在moveToFirst上崩溃不迟,我已经在回答中添加了代码.

非常感谢

kde*_*iry 5

本机代码抛出此异常.(参考这个)

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

  1. 仔细阅读您的代码并确保您在finally条款中关闭所有游标.
  2. 如果从数据库中提取的数据可能过大,请将它们放在单独的块中(比如在一个列表中执行拉取以获取更多内容).

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