Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 光标窗口分配2048 kb失败. #Open Cursors = 1(此proc = 1打开的#游标)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作一个使用数据库的Kiosk应用程序.该应用程序一直在前台运行.该应用程序有许多使用一个共享DataBaseHelper实例的线程.该应用程序完美无缺,但大多数时间在5或6小时后,我遇到这些异常,然后应用程序崩溃:

我正确关闭了光标,但仍然获得了这些异常.那些例外是什么?这些例外的原因是什么?

主要活动

private DataBaseHelper db = null; // Global
@H_459_2@mainActivity onCreate方法

db = new DataBaseHelper(this);

new Thread(new Runnable() {
@Override
public void run() {
    while (threadRunningFlag) {

    cursor result = null;
    try {

        result = db.getData("SELECT " + DataBaseHelper.SMS_columN_PHONE_numbER  + " FROM " + DataBaseHelper.SMS_TABLE_NAME + " LIMIT 5");
        if (result != null && result.getCount() > 0) {
            while (!result.isAfterLast()) {
                String phonenumber = result.getString(result.getcolumnIndex(DataBaseHelper.SMS_columN_PHONE_numbER));
                // ...
                result.moveToNext();
            }
        }
    }catch (Exception E) {
        Log.e(tag,"err->" + e.getLocalizedmessage());

    }finally {
        if (result != null) {
            result.close();
            result = null;
        }
    }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException E) {
            Log.e(tag,e.getmessage());
        }
    }
}
}).start();

DataBaseHelper类:

public class DataBaseHelper extends sqliteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "YadProjectDB.db";
    public static final String SMS_TABLE_NAME = "sms";
    public static final String SMS_columN_PHONE_numbER = "number";
    public static final String SMS_columN_SMS_TEXT = "message";

    public static final String BLACK_LIST_TABLE_NAME = "blackList";
    public static final String BLACK_LIST_COLUMN_ID = "id";
    public static final String BLACK_LIST_columN_PHONE_numbER = "number";

    private final String TAG = "DataBaseHelper";

    public DataBaseHelper(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);

    }

    @Override
    public void onCreate( sqliteDatabase db ) {
        String command = "create table "
                + SMS_TABLE_NAME
                + "("
                + SMS_columN_PHONE_numbER + " TEXT,"
                + SMS_columN_SMS_TEXT + " TEXT,"
                + ")";
        try {
            db.execsql(command);
        }catch (Exception E) {
            Log.e(tag,"err->" + e.getmessage());
        }

        command = "create table "
                + BLACK_LIST_TABLE_NAME
                + "("
                + BLACK_LIST_columN_PHONE_numbER + " TEXT,"err->" +  e.getmessage());
        }
    }

    public Boolean insertToSms(String number,String message,String filename,Integer uploadFlag,Integer blackList,Integer pictureFlag)
    {
        final sqliteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(SMS_columN_PHONE_numbER,number);
        contentValues.put(SMS_columN_SMS_TEXT,messagE);
        try {
            db.insert(SMS_TABLE_NAME,contentValues);
            return true;
        }catch (Exception E) {
            Log.e(tag,"err->" + e.getmessage());
            return false;
        }
    }

    public cursor getData(String query){
        final sqliteDatabase db = getReadableDatabase();
        cursor res = null;
        try {
            res = db.rawQuery(query,null);
            res.moveToFirst();
            return res;
        }catch (Exception E) {
            Log.e(tag,"err->" + e.getmessage());
            if (res != null) {
                res.close();
                res = null;
            }
        }
        return null;
    }
}

解决方法

这与 SQLite Android Database Cursor window allocation of 2048 kb failed看起来相同

你的错误说:“打开游标

上述问题的答案解释了:

cursor cursor = null;
try {
    cursor = db.query(...
    // do some work with the cursor here.
} finally {
    // this gets called even if there is an exception somewhere above
    if(cursor != null)
        cursor.close();
}

大佬总结

以上是大佬教程为你收集整理的android – 光标窗口分配2048 kb失败. #Open Cursors = 1(此proc = 1打开的#游标)全部内容,希望文章能够帮你解决android – 光标窗口分配2048 kb失败. #Open Cursors = 1(此proc = 1打开的#游标)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。