关于CursorWindowAllocatoinException有很多关于SO的问题:

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

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类可能有问题.这是代码:

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);

}

}

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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐