【问题标题】:android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed even after closing cursorandroid.database.CursorWindowAllocationException:即使在关闭光标后,2048 kb 的光标窗口分配也失败
【发布时间】:2013-11-16 10:46:49
【问题描述】:

关于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);
    }
}

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

【问题讨论】:

    标签: android sqlite memory memory-leaks android-cursor


    【解决方案1】:

    我正在从服务中查询数据库。当我从一个活动中做到这一点时,问题就再也没有发生过。

    除了查询之外,我还从同一服务中的串行端口读取数据。也许这导致了问题。但是,当我使用活动而不是服务时,问题再也没有发生过。

    【讨论】:

    • 我没有得到答案。我有同样的问题
    【解决方案2】:

    我的应用程序在从 android 媒体商店加载 500 多首歌曲时遇到了同样的问题。

    关闭光标并不能解决您的问题。

    您应该使用AsyncTask.。 在 doInBackground 方法中编写从光标检索值的代码。

    公共类 CursorLoader 扩展 AsyncTask {

        @Override
        protected Void doInBackground(Void... params) {
    
            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();
    }
    
            return null;
        }
    
        @Override
        protected void onPreExecute() {
    
        }
    
    
    
        @Override
        protected void onPostExecute(Void result) {
    
    
    }
    

    【讨论】:

      【解决方案3】:

      我在使用永久循环的服务类中调用 DATA BASE 时遇到了同样的问题。

      我的代码是这样的:

      service ()
       {
      void run()
         {
           while(conditionisTrue)
             {
                    db.getRecord();  //I remove this code from here and put it in the start of service c
              }
          }
       }
      

      【讨论】:

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