Android 数据库的SQLiteConnection对象已泄漏
似乎每隔一次我测试这个应用程序时,我都会收到一条消息,“数据库[path]的SQLiteConnection对象泄漏!请修复你的应用程序,以正确结束正在进行的事务,并在不再需要时关闭数据库。” 我在这里看到了其他的问题,可以解决这个问题,但我不相信任何答案符合我的情况。首先,我所有的数据库访问都是通过内容提供商进行的。另一方面,我正在处理不止一个数据库(两个都被泄露),以及许多保存其中一个数据库数据的游标 我会发布内容提供者,但我的内容提供者有1200行。让我知道我应该挑选哪些部分,我会在中编辑它们。如果相关,则在内容提供程序中有一个SQLiteOpenHelper内部类。否则,下面是一个可能导致此问题的活动的片段:Android 数据库的SQLiteConnection对象已泄漏,android,sqlite,memory-leaks,android-contentprovider,Android,Sqlite,Memory Leaks,Android Contentprovider,似乎每隔一次我测试这个应用程序时,我都会收到一条消息,“数据库[path]的SQLiteConnection对象泄漏!请修复你的应用程序,以正确结束正在进行的事务,并在不再需要时关闭数据库。” 我在这里看到了其他的问题,可以解决这个问题,但我不相信任何答案符合我的情况。首先,我所有的数据库访问都是通过内容提供商进行的。另一方面,我正在处理不止一个数据库(两个都被泄露),以及许多保存其中一个数据库数据的游标 我会发布内容提供者,但我的内容提供者有1200行。让我知道我应该挑选哪些部分,我会在中编辑
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[])
这段代码由多个微调器调用,以获取将填充它们的数据行。如果您想知道的话,不,光标不会关闭,但是我找不到一种方法来关闭它而不丢失适配器中的数据
因此,请让我知道您的任何想法,或对内容提供商部分内容的建议
编辑:这是日志
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()
我相信,关于游标的行对于这个尝试来说是新的,但是正如我所说的,当访问游标仅对上面的方法可用时,我不知道有什么方法可以关闭游标。此外,根据尝试的不同,也可能会有文字表明另一个数据库(通过应用程序的不同部分访问,但使用相同的内容提供商)也被泄漏
编辑:
以下是内容提供者中查询方法的相关部分(如果有帮助的话)
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
,因为这是我找到的唯一一种方法,可以让我访问与微调器项关联的ID。发布显示错误/异常的日志。简单地在任何上下文中显示几行关于如何使用它的内容,并不能帮助任何人解决您的问题。@Squonk正如我确信您所读到的,我已经意识到我需要编辑更多信息。我已经知道我发布的这些信息可能不会有什么帮助,但我只需要知道还有什么需要补充的。感谢添加LogCat条目的建议。首先,在您获得数据后,您如何将其显示到UI中,这样我们可以确定您是否使用了正确类型的光标,我的猜测可能不是,因为它只是一个光标。对于具有某种列表视图的数据库,至少应使用SimpleCrsorAdapter。@PeterBirdsall我添加了一段代码,用于将微调器内容(通过设置适配器内容)设置为光标的内容。