问题分析:
在打开数据库操作之前,应用程序没有关闭游标或数据库对象。
解决方案:
应用程序没有关闭游标或数据库对象导致抛出此类异常。通过对游标及数据库对之前打开的数据库对象调用close方法进行关闭操作即可。
示例:
- [backcolor=transparent] android[backcolor=transparent].[backcolor=transparent]database[backcolor=transparent].[backcolor=transparent]sqlite[backcolor=transparent].[backcolor=transparent]DatabaseObjectNotClosedException[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]Application[backcolor=transparent] did [backcolor=transparent]not[backcolor=transparent] close the cursor [backcolor=transparent]or[backcolor=transparent] database [backcolor=transparent]object[backcolor=transparent] that was opened here
- [backcolor=transparent] android[backcolor=transparent].[backcolor=transparent]database[backcolor=transparent].[backcolor=transparent]sqlite[backcolor=transparent].[backcolor=transparent]DatabaseObjectNotClosedException[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]Application[backcolor=transparent] did [backcolor=transparent]not[backcolor=transparent] close the cursor [backcolor=transparent]or[backcolor=transparent] database [backcolor=transparent]object[backcolor=transparent] that was opened here
- [backcolor=transparent] at android[backcolor=transparent].[backcolor=transparent]content[backcolor=transparent].[backcolor=transparent]ContentResolver[backcolor=transparent].[backcolor=transparent]query[backcolor=transparent]([backcolor=transparent]ContentResolver[backcolor=transparent].[backcolor=transparent]java[backcolor=transparent]:[backcolor=transparent]399[backcolor=transparent])
- [backcolor=transparent] at android[backcolor=transparent].[backcolor=transparent]content[backcolor=transparent].[backcolor=transparent]ContentResolver[backcolor=transparent].[backcolor=transparent]query[backcolor=transparent]([backcolor=transparent]ContentResolver[backcolor=transparent].[backcolor=transparent]java[backcolor=transparent]:[backcolor=transparent]316[backcolor=transparent])
步骤1:
使用try catch块捕获异常,并在finally部分关闭数据库。
- [backcolor=transparent] [backcolor=transparent]OpenHelper[backcolor=transparent] dh [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]DatabaseHelper[backcolor=transparent]([backcolor=transparent]this[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]SQLiteDatabase[backcolor=transparent] db [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]try[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] db [backcolor=transparent]=[backcolor=transparent] dh[backcolor=transparent].[backcolor=transparent]getWritableDatabase[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]catch[backcolor=transparent]([backcolor=transparent]SQLiteException[backcolor=transparent] e[backcolor=transparent]){
- [backcolor=transparent] e[backcolor=transparent].[backcolor=transparent]printStackTrace[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]finally[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent]([backcolor=transparent]db [backcolor=transparent]!=[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent] [backcolor=transparent]&&[backcolor=transparent] db[backcolor=transparent].[backcolor=transparent]isOpen[backcolor=transparent]())
- [backcolor=transparent] db[backcolor=transparent].[backcolor=transparent]close[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}
步骤2:
在Activity生命周期中,onResume()打开数据库,onPause()关闭数据库。
- [backcolor=transparent] [backcolor=transparent]OpenHelper[backcolor=transparent] dh [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]protected[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onCreate[backcolor=transparent]([backcolor=transparent]Bundle[backcolor=transparent] savedInstanceState[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] dh [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]DatabaseHelper[backcolor=transparent]([backcolor=transparent]this[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]protected[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onResume[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]this[backcolor=transparent].[backcolor=transparent]db [backcolor=transparent]=[backcolor=transparent] dh[backcolor=transparent].[backcolor=transparent]getWritableDatabase[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]protected[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onPause[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent]([backcolor=transparent]db [backcolor=transparent]!=[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent] [backcolor=transparent]&&[backcolor=transparent] db[backcolor=transparent].[backcolor=transparent]isOpen[backcolor=transparent]()){
- [backcolor=transparent] db[backcolor=transparent].[backcolor=transparent]close[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
参考文献:
- http://stackoverflow.com/questions/7492006/databaseobjectnotclosedexception
- http://stackoverflow.com/questions/4734886/how-can-one-avoid-databaseobjectnotclosedexception
- http://debuglog.iteye.com/blog/1404338