3

似乎每隔一次我测试这个应用程序时,我都会收到消息,“数据库 [path] 的 SQLiteConnection 对象已泄漏!请修复您的应用程序以正确结束正在进行的事务并在不再需要时关闭数据库。 "

我在 SO 上看到了解决此消息的其他问题,但我认为任何答案都不适合我的情况。一方面,我所有的数据库访问都是通过 Content Provider 进行的。另一方面,我正在处理多个数据库(两者都被泄露),以及许多保存来自其中一个数据库的数据的游标。

我会发布内容提供者,但我的内容提供者有 1200 行长。让我知道我应该选择哪些部分,我将在其中进行编辑。如果相关,则 Content Provider 中有一个 SQLiteOpenHelper 内部类。否则,这是似乎导致此问题的活动的一部分:

private SimpleCursorAdapter setFixedSpinnerAdapter(String table, String[] columns){
        Uri uri = Uri.parse(DBContentProvider.CONTENT_URI_OPTION + table);
        String sortOrder = (columns.length > 2 ? columns[2] : columns[1]);
        //if 3rd column specified, use it for sorting
        Cursor cursor = getContentResolver().query(uri, columns, null, null, sortOrder);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, 
                cursor, new String[]{cursor.getColumnName(1)}, new int[]{android.R.id.text1}, 0);
        return adapter;
    }// method: setFixedSpinnerAdapter(String, String[])    

这段代码被多个 Spinner 调用以获取将填充它们的数据行。如果您想知道,没有光标不会关闭,但我找不到在不丢失适配器数据的情况下关闭它的方法。

因此,让我知道您的任何想法,或内容提供者的部分建议包括在内。

编辑:这是logcat

07-15 13:24:42.162: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.162: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.162: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.162: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.162: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.162: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.170: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.170: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.170: W/SQLiteConnectionPool(7394): A SQLiteConnection object for database '+data+data+edu_dordt_grassrootsplantid+databases+plant_identification_local' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
07-15 13:24:42.170: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.178: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.178: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.178: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.178: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.178: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.178: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.178: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.186: W/CursorWrapperInner(7394): Cursor finalized without prior close()
07-15 13:24:42.186: W/CursorWrapperInner(7394): Cursor finalized without prior close()

我相信,关于光标的行对于这次尝试来说是新的,但正如我所说,当只能通过上述方法访问光标时,我不知道有任何方法可以关闭光标。此外,根据尝试的不同,也可能有行说另一个数据库(通过应用程序的不同部分访问,但使用相同的 Content Provider)也被泄露。

编辑:这是内容提供者中查询方法的相关部分,如果它有帮助的话。

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder){
        String dbName;
        Cursor cursor;
        String tables;
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        switch(uriMatcher.match(uri)){
            case CODE_OPTION_SEARCH://Called from setFixedSpinnerAdapter()
                dbName = DATABASE_PLANT_IDENTIFICATION_LOCAL;
                dbHelper = new DatabaseHelper(getContext(), dbName); 
                tables = uri.getLastPathSegment();
                db = dbHelper.getReadableDatabase();
                queryBuilder.setTables(tables);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI");
        }//switch(uriMatcher.match(uri))
        cursor = queryBuilder.query(db, projection, null, null, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;
    }// method: ContentProvider.query(Uri, String[], String, String[], String)

编辑:我多次使用这种格式的代码,用于不同的微调器。

spnGrowthForm.setAdapter(setFixedSpinnerAdapter(
    DBContentProvider.TABLE_GROWTH_FORM_OPTION, 
    new String[]{DBContentProvider.KEY_GROWTH_FORM_ID,
    DBContentProvider.KEY_GROWTH_FORM_TEXT}));

该方法setFixedSpinnerAdapter(String, String[])与顶部发布的方法相同。我使用了 ,SimpleCursorAdapter因为这是我发现让我访问与 Spinner 项目关联的 ID# 的唯一方法。

4

0 回答 0