开发者

What is "SQLiteDatabase created and never closed" error?

开发者 https://www.devze.com 2023-04-09 22:06 出处:网络
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

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.disp开发者_C百科atchMessage(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)


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?


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

db.close()


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


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();
 }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号