android.database.CursorWindowAllocationException:2048 KB光标窗口配置即使在关闭游标后失败游标、光标、窗口、database

2023-09-05 02:59:24 作者:半生浮名

有对SO有关CursorWindowAllocatoinException许多问题:

There are many questions on SO about CursorWindowAllocatoinException:

SQLite 2048 KB Android的数据库光标窗口分配失败 无法分配CursorWindow 内存的分配游标时 Android MySQLite CursorWindowAllocationException崩溃 SQLite Android Database Cursor window allocation of 2048 kb failed Could not allocate CursorWindow Out of Memory when allocating cursors Android MySQLite CursorWindowAllocationException crash

所有这些表明光标在使用后必须封闭。但是,这并没有解决我的问题。这是我的code:

All of them suggest that cursor must be closed after use. But that did not resolve my problem. Here is my code:

String query = "select serial from tbl1 union select serial from tbl2 union select serial from tbl3";
    SQLiteDatabase db = null;
    Cursor cur = null;
    try {
        SettingsDatabaseHelper dal = new SettingsDatabaseHelper(
                c);
        db = dal.getReadableDatabase();
        cur = db.rawQuery(query, null);
        int numRows = cur.getCount();
        if (numRows > 0) {
            cur.moveToFirst();

            int serialIdx = cur.getColumnIndexOrThrow("serial");
            for (boolean hasItem = cur.moveToFirst(); hasItem; hasItem = cur
                    .moveToNext()) {

                String serial = cur.getString(serialIdx);
                if (Validator.getInstance().isValidSerial(serial))
                    serials.add(serial);

            }
        }
    } finally {
        if (cur != null)
            cur.close();

        if (db != null)
            db.close();
    }

我跑这个方法每隔几秒钟。半个多小时后,我得到 CursorWindowAllocationException INT = numRows行cur.getCount(); ,然后我的服务停止。

I run this method every few seconds. After half an hour, I get CursorWindowAllocationException in int numRows=cur.getCount(); and then my service stops.

由于我关闭游标,有可能是与我的助手类的问题。这里是code:

Since I am closing the cursor, there may be a problem with my Helper class. Here is the code:

public class SettingsDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "mydb.db";
    private static final int DATABASE_VERSION = 1;


    private static final String TBL_CREATE="create table...";



    public SettingsDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(TBL_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onCreate(db);
    }
}

我怎样才能prevent此异常被抛出?

How can I prevent this exception from being thrown?

推荐答案

我是从一个服务查询数据库。当我从一个活动做到了,这个问题不会再发生了。

I was querying the database from a service. When I did it from an activity, the problem never occurred again.

除了查询,我从串口读取相同的服务范围内。也许这造成了问题。然而,当我使用的服务的活动,而不是,这个问题永远不会再次发生。

Alongside querying, I was reading from the serial port within the same service. Maybe that caused the problem. However, when I used an activity instead of the service, the problem never happened again.