【问题标题】:Cursor window allocation of 2048 kb failed running with background service (runnable)2048 kb 的光标窗口分配与后台服务(可运行)一起运行失败
【发布时间】:2018-02-14 02:23:46
【问题描述】:

我在从 SQLite 获取数据时遇到问题2048 kb 的光标窗口分配失败使用 Thread 后台服务

但是如果我在没有 Thread 后台服务的情况下运行 List branchList= DBTransaction.BranchList();,这个函数是正常工作的

MyService.Java

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Service onStartCommand");
    new Thread(new Runnable() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void run() {
            while(true)
            {
                List<Branch> branchList= DBTransaction.BranchList();
                if (branchList.size() < 0)
                    getBranch(); //get branch using volley get data from web service

                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
    return START_STICKY;
}

SQLite 获取数据

public List<Branch> BranchList(){
    List<Branch> branchList = new ArrayList<Branch>();
    String selectQuery = "SELECT * FROM " + TABLE_BRANCH;

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()){
            do{
                Branch branch = new Branch();
                branch.setID(cursor.getString(0));
                branch.setName(cursor.getString(1));
                branch.setCode(cursor.getString(2));

                branchList.add(branch);
            }while (cursor.moveToNext());
        }
    }finally { 
         if(cursor != null)
         cursor.close();
    }
    return branchList;
}

来自安卓监视器的错误结果

E/Volley: [10497] NetworkDispatcher.run: Unhandled exception android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=8 (# cursors opened by this proc=8)
                                                android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=8 (# cursors opened by this proc=8)

【问题讨论】:

标签: android sqlite mobile memory-leaks cursor


【解决方案1】:

“由于错误......无法分配大小为 ### 的 CursorWindow ....”
当游标未关闭并在循环中调用时发生。

如果在您的代码中添加 cursor.close(); 不能解决您的问题,
应用我在此答案中给出的严格规则https://stackoverflow.com/a/65827816/2173890,您将被指向导致问题的行。

public List<Branch> BranchList(){
    List<Branch> branchList = new ArrayList<Branch>();
    String selectQuery = "SELECT * FROM " + TABLE_BRANCH;

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()){
            do{
                Branch branch = new Branch();
                branch.setID(cursor.getString(0));
                branch.setName(cursor.getString(1));
                branch.setCode(cursor.getString(2));

                branchList.add(branch);
            }while (cursor.moveToNext());
        }

cursor.close();//如图添加这一行。

    }finally { 
         if(cursor != null)
         cursor.close();
    }
    return branchList;
}

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多