詳細異常:
A SQLiteConnection object for database '/data/data/.../databases/....db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed
明顯是數據庫操作異常,數據庫對象被鎖,明確告訴你對象長久不用需要關閉。
改正:獲取數據庫對象改成單例模式,項目運行中只保證唯一一個對象即可。如下:
private static XXXXSQLHelper mInstance = null;
public synchronized static XXXXSQLHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new XXXXSQLHelper(context);
}
return mInstance;
};
調用如下:
public XXXXDBUtil(Context context) {
mSQLiteDatabase = XXXXSQLHelper.getInstance(context)
.getWritableDatabase();
}
注意:此時數據庫對象是唯一實例了,不需要close了,如果close掉,將會出現對象已關閉的嚴重異常,導致程序崩潰。