android.database.CursorWindowAllocationException:光标窗口分配2048 kb失败,即使关闭游标

前端之家收集整理的这篇文章主要介绍了android.database.CursorWindowAllocationException:光标窗口分配2048 kb失败,即使关闭游标前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
关于CursorWindowAllocatoinException有很多关于SO的问题:

> SQLite Android Database Cursor window allocation of 2048 kb failed
> Could not allocate CursorWindow
> Out of Memory when allocating cursors
> Android SQLite CursorWindowAllocationException crash

他们都建议游标在使用后必须关闭.但这并没有解决我的问题.这是我的代码

@H_403_9@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(); }

我每隔几秒钟运行一次这个方法.半小时后,我在int numRows = cur.getCount()中得到CursorWindowAllocationException;然后我的服务停止.

由于我关闭游标,所以我的Helper类可能有问题.这是代码

@H_403_9@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); } }

如何防止这个异常被抛出?

解决方法

我正在从一个服务查询数据库.当我从一个活动中做到这一点,这个问题从未再次发生.

除了查询,我正在从同一个服务中的串行端口读取.也许这导致了这个问题.但是,当我使用活动而不是服务时,问题从未再次发生.

猜你在找的Android相关文章