Java Getiing android.database.CursorWindowAllocationException经常崩溃
除此之外,已经探讨了其他主题。就我而言,我已经在适当的时间关闭了游标。因此,如果(cursor!=null&&cursor.getCount()>0),则很少会从这个中获得此异常。请找到下面的代码,并需要您的专家对此案例的建议Java Getiing android.database.CursorWindowAllocationException经常崩溃,java,android,android-sqlite,Java,Android,Android Sqlite,除此之外,已经探讨了其他主题。就我而言,我已经在适当的时间关闭了游标。因此,如果(cursor!=null&&cursor.getCount()>0),则很少会从这个中获得此异常。请找到下面的代码,并需要您的专家对此案例的建议 List<User> itemList = new ArrayList<>(); String selectQuery = "SELECT * FROM " + TABLE_USERS; try (SQLi
List<User> itemList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_USERS;
try (SQLiteDatabase db = this.getWritableDatabase()) {
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
User item = new User();
item.setId(cursor.getInt(0));
item.setUserPhoneNumber(cursor.getString(1));
itemList.add(item);
cursor.moveToNext();
}
}
cursor.close();
}
} catch (SQLiteException ex) {
Log.e(TAG, ex.toString());
}
return itemList;
List itemList=new ArrayList();
String selectQuery=“SELECT*FROM”+表格用户;
try(SQLiteDatabase db=this.getWritableDatabase()){
Cursor Cursor=db.rawQuery(selectQuery,null);
if(cursor!=null&&cursor.getCount()>0){
if(cursor.moveToFirst()){
而(!cursor.isAfterLast()){
用户项=新用户();
item.setId(cursor.getInt(0));
item.setUserPhoneNumber(cursor.getString(1));
项目列表。添加(项目);
cursor.moveToNext();
}
}
cursor.close();
}
}catch(SQLiteException-ex){
Log.e(TAG,例如toString());
}
返回项目列表;
我建议更改您的代码以使用:-
List<User> itemList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getWritableDatabase()
Cursor cursor = db.rawQuery(selectQuery, null);
while (cursor.moveToNext) {
User item = new user();
item.setId(cursor.getint(0));
item.setUserPhoneNumber(cursor.getString(1));
itemList.add(item);
}
csr.close();
return itemList;
- 。。。。未按原样编码,它表示原始代码的其余部分
另一个潜在问题是打开的游标太多。最大数量约为1000。每个光标都位于一个文件之下,这里的限制是可以一次打开的文件(文件句柄)的数量(因为其他打开的文件会影响打开的光标的最大数量)。因此,当完成游标时,您应该始终关闭它们(从您给出的答案来看,您似乎已经意识到了这一点)。在您的代码中,空光标永远不会关闭,这可能会导致太多光标被打开。我展示的代码只有在应用程序崩溃时才会关闭光标,在这种情况下,打开的光标才会关闭。同样,使用建议的代码总是会关闭光标,因此根据建议采用此代码或代码可以消除此问题。感谢您宝贵的回复。我正在按照建议重组我的职能。在实际代码中,我为用户获取所有数据,并在用户模型中存储相同的数据,因为所有列都是必需的。在我的应用程序SQLite数据库中,此处仅存储文本和数字。但是,对于某些用户,此查询返回1300行。然后只有我得到了这个场景。内存超过4MB。我也在尝试复制同样的东西。
SELECT * FROM ....
SELECT id_column_name, phoneNumber_column_name FROM ....