Android SQLite DatabaseObjectNotClosedException
Android SQLite DatabaseObjectNotClosedException,android,sqlite,sqliteopenhelper,Android,Sqlite,Sqliteopenhelper,在我正在构建的PoC Android应用程序中,当应用程序在关闭时调用垃圾收集器时,我得到一个Android.database.sqlite.DatabaseObjectNotClosedException。甚至当我在SQLiteDatabase以及我正在使用的SQLiteOpenHelper对象上调用close方法时,这种情况也会发生 我正在使用一个DAO(从活动的AsyncTask.doInBackground调用)访问数据库,该DAO保存用于在构造函数中打开数据库的代码,该构造函数是从活动
在我正在构建的PoC Android应用程序中,当应用程序在关闭时调用垃圾收集器时,我得到一个
Android.database.sqlite.DatabaseObjectNotClosedException
。甚至当我在SQLiteDatabase
以及我正在使用的SQLiteOpenHelper
对象上调用close方法时,这种情况也会发生
我正在使用一个DAO(从活动的AsyncTask.doInBackground
调用)访问数据库,该DAO保存用于在构造函数中打开数据库的代码,该构造函数是从活动的onCreate()
方法调用的
//Code in my DAO class
public MyDataSource(Context context) {
dbHelper = new MyOpenHelper(context);
dbHelper.createDatabase();
}
//Calling Activity
MyDataSource myDs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.create);
myDs= new MyDataSource (MyActivity.this);
}
@Override
public void onPause() {
investmentDs.close();
if(ConstantValues.DebugModeManager.DEBUG_CREATE || ConstantValues.DebugModeManager.DEBUG_MODE) {
Log.d(LOG_TAG, "Create screen paused...");
}
super.onPause();
}
我还有一个关闭SQLiteDatabase
以及SQLiteOpenHelper
对象的方法,它是从同一活动的onPause()
方法调用的。数据库读/写是在异步任务的doInBackground
方法中执行的
我已经覆盖了SQLiteOpenHelper
的close()
方法,如下所示:
@Override
public synchronized void close() {
if(database != null)
database.close();
super.close();
}
我从DAO类中的close()
方法调用它
如果需要进一步澄清,请告诉我。
谢谢,
德博吉特
编辑1:
这是从数据库获取的代码
cursor = db.query("EMPLOYEES", null, whereClause, null, null, null, "EMPLOYEE_ID");
cursor.moveToFirst();
List<MyBean> beans = new ArrayList<MyBean>();
while(!cursor.isAfterLast()) {
beans.add(cursorToObject(cursor)); //this fetches data from the cursor's current row
cursor.moveToNext(); // and inserts into a POJO of type MyBean
}
cursor.close();
cursor=db.query(“EMPLOYEES”,null,where子句,null,null,EMPLOYEES_ID”);
cursor.moveToFirst();
listbeans=newarraylist();
而(!cursor.isAfterLast()){
add(cursorToObject(cursor));//从游标的当前行获取数据
cursor.moveToNext();//并插入MyBean类型的POJO
}
cursor.close();
光标对象未关闭。
无论何时完成游标对象调用Cursor.close()在获取数据并将其加载到数组列表后,我将立即关闭光标。