在我的数据库助手中,我已经关闭了游标和数据库连接,但是 logcat 仍然说有泄漏!
我的 SQLiteOpenHelper 中有这段代码
// Getting all entries from database table wp_posts
public ArrayList<String> getAllPosts() {
// Open the database
String path_to_db = context.getDatabasePath(db_name).getPath();
db = SQLiteDatabase.openDatabase(path_to_db, null, SQLiteDatabase.OPEN_READONLY);
// Prepare the container array
ArrayList<String> postsArrayList = new ArrayList<String>();
// select all posts
String selectQuery = "SELECT post_title FROM " + wp_posts + " ORDER BY post_title COLLATE NOCASE";
Cursor c = db.rawQuery(selectQuery, null);
//Log.e("DataBaseHelper", "getAllPosts => line 205");
// looping through all rows and adding to list
while (c.moveToNext()) {
postsArrayList.add(c.getString(0));
}
// when you're done close the cursor
c.close();
// ..then close the database
db.close();
//Log.e("DataBaseHelper", "getAllPosts => line 218");
return postsArrayList;
}
Logcat 日志
09-22 21:29:24.617: E/DataBaseHelper(17090): DataBaseHelper => 84
09-22 21:29:24.807: D/dalvikvm(17090): GC_FOR_ALLOC freed 78K, 5% free 2895K/3024K, paused 26ms, total 28ms
09-22 21:29:24.807: W/SQLiteConnectionPool(17090): A SQLiteConnection object for database
'/data/data/net.example/databases/atest.db' was leaked!
Please fix your application to end transactions in progress properly and to close
the database when it is no longer needed.
09-22 21:29:24.947: D/dalvikvm(17090): GC_FOR_ALLOC freed 65K, 4% free 3343K/3460K, paused 1ms, total 2ms
09-22 21:29:25.027: E/DataBaseHelper(17090): DataBaseHelper => 88
在主要活动中,我使用以下方法调用助手:
public void fillListView(ListView listview, DataBaseHelper db) {
Log.e("DataBaseHelper", "DataBaseHelper => 84");
// Get the entries from db table; wp_posts
ArrayList<String> entries = db.getAllPosts();
Log.e("DataBaseHelper", "DataBaseHelper => 88");
// ..then close the database
db.close();
// ArrayAdapter points to list_view.xml; which doesn't contain
// LinearLayout
// but only a TextView. This will be used as adapter
adapter = new ArrayAdapter<String>(this, R.layout.db_row_textview, entries);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close DB
db.close();
}
我究竟做错了什么?谢谢。