开发者社区> 问答> 正文

android_database_sqlite_DatabaseObjectNotClosedException该如何修改?


问题分析:
在打开数据库操作之前,应用程序没有关闭游标或数据库对象。
解决方案:
应用程序没有关闭游标或数据库对象导致抛出此类异常。通过对游标及数据库对之前打开的数据库对象调用close方法进行关闭操作即可。
示例:

  1. [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
  2. [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
  3. [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])
  4. [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部分关闭数据库。
  1. [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]);
  2. [backcolor=transparent]  [backcolor=transparent]SQLiteDatabase[backcolor=transparent] db [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
  3. [backcolor=transparent]  [backcolor=transparent]try[backcolor=transparent] [backcolor=transparent]{
  4. [backcolor=transparent]      db [backcolor=transparent]=[backcolor=transparent]  dh[backcolor=transparent].[backcolor=transparent]getWritableDatabase[backcolor=transparent]();
  5. [backcolor=transparent]  [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]catch[backcolor=transparent]([backcolor=transparent]SQLiteException[backcolor=transparent] e[backcolor=transparent]){
  6. [backcolor=transparent]      e[backcolor=transparent].[backcolor=transparent]printStackTrace[backcolor=transparent]();
  7. [backcolor=transparent]  [backcolor=transparent]}[backcolor=transparent] [backcolor=transparent]finally[backcolor=transparent] [backcolor=transparent]{
  8. [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]())
  9. [backcolor=transparent]          db[backcolor=transparent].[backcolor=transparent]close[backcolor=transparent]();
  10. [backcolor=transparent]  [backcolor=transparent]}

步骤2:
在Activity生命周期中,onResume()打开数据库,onPause()关闭数据库。
  1. [backcolor=transparent]  [backcolor=transparent]OpenHelper[backcolor=transparent] dh [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent];
  2. [backcolor=transparent]  [backcolor=transparent]@Override
  3. [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]{
  4. [backcolor=transparent]      dh [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]DatabaseHelper[backcolor=transparent]([backcolor=transparent]this[backcolor=transparent]);
  5. [backcolor=transparent]  [backcolor=transparent]}
  6. [backcolor=transparent]  [backcolor=transparent]@Override
  7. [backcolor=transparent]  [backcolor=transparent]protected[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onResume[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
  8. [backcolor=transparent]      [backcolor=transparent]this[backcolor=transparent].[backcolor=transparent]db [backcolor=transparent]=[backcolor=transparent]  dh[backcolor=transparent].[backcolor=transparent]getWritableDatabase[backcolor=transparent]();
  9. [backcolor=transparent]  [backcolor=transparent]}
  10. [backcolor=transparent]  [backcolor=transparent]@Override
  11. [backcolor=transparent]  [backcolor=transparent]protected[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] onPause[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]{
  12. [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]()){
  13. [backcolor=transparent]          db[backcolor=transparent].[backcolor=transparent]close[backcolor=transparent]();
  14. [backcolor=transparent]      [backcolor=transparent]}
  15. [backcolor=transparent]  [backcolor=transparent]}

参考文献:
  1. http://stackoverflow.com/questions/7492006/databaseobjectnotclosedexception
  2. http://stackoverflow.com/questions/4734886/how-can-one-avoid-databaseobjectnotclosedexception
  3. http://debuglog.iteye.com/blog/1404338

展开
收起
猫饭先生 2017-10-25 10:24:13 1437 0
0 条回答
写回答
取消 提交回答
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载