What is “SQLiteDatabase created and never closed” error?

北战南征 提交于 2019-12-10 20:02:40

问题


I have closed the database in my adapter class, so whay is this error showingup on logcat but my application is not forcr closing but only error is showing on log cat..where i shuold have to close the database for ignoring this error...?

my errors are...below..in which class i left for closing the database.....i took help from this link http://www.vogella.de/articles/AndroidSQLite/article.html

ERROR/Database(265): Leak found
      ERROR/Database(265): java.lang.IllegalStateException: /data/data/expenceanywhere.mobile/databases/data SQLiteDatabase created and never closed
      ERROR/Database(265):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1581)
      ERROR/Database(265):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:638)
      ERROR/Database(265):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:659)
      ERROR/Database(265):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:652)
      ERROR/Database(265):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:482)
       ERROR/Database(265):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
       ERROR/Database(265):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
        ERROR/Database(265):     at expenceanywhere.mobile.NotesDbAdapter.open(NotesDbAdapter.java:56)
        ERROR/Database(265):     at expenceanywhere.mobile.AddEditExpense.onCreate(AddEditExpense.java:199)
        ERROR/Database(265):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
        ERROR/Database(265):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
       ERROR/Database(265):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
       ERROR/Database(265):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
         ERROR/Database(265):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
         ERROR/Database(265):     at android.os.Handler.dispatchMessage(Handler.java:99)
         ERROR/Database(265):     at android.os.Looper.loop(Looper.java:123)
         ERROR/Database(265):     at android.app.ActivityThread.main(ActivityThread.java:4203)
       ERROR/Database(265):     at java.lang.reflect.Method.invokeNative(Native Method)
       ERROR/Database(265):     at java.lang.reflect.Method.invoke(Method.java:521)
       ERROR/Database(265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
         ERROR/Database(265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
          ERROR/Database(265):     at dalvik.system.NativeStart.main(Native Method)

回答1:


Remember to always close db in Helper class

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

The similar question: Sqlite Database LEAK FOUND exception in android?




回答2:


You have to close the database before exiting the application or exiting the class.

db.close()



回答3:


You need to close any open database object with calling the close() method.




回答4:


I confirm the problem with the leakage in the code from Voggella. What worked in my case was overriding OnDestroy method with a close statement as others proposed. I guess you can add this code in your AddEditExpense.java.

 @Override  
 protected void onDestroy() {
   super.onDestroy();
   dbHelper.close();
 }



回答5:


I had the same problem in one of my apps. I opened and closed the database every time I accessed it. But I still got the error. I suppose different threads which access the shared object messes up the database status in this approach.

To overcome this I placed the database instance in the main Activity and shared it to all other activities. in the OnCreate() method of the main activity the db instance is created. I had to override the onResume() and onPause() methods which I closed the database (within onPause()) and opened (within onResume()) of the main activity.

I'm not sure whether this is the correct way of getting rid of the problem, but it has worked for me so far.



来源:https://stackoverflow.com/questions/7632047/what-is-sqlitedatabase-created-and-never-closed-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!