大家好,我是新手,在使用greenDAO来管理数据库。但是在Application onTerminate()中经常会遇到数据库未关闭的异常。我只维护应用程序中的单个数据库连接,但在某些情况下,当我的应用程序进入后台并恢复时,数据连接对象会变为空。我通过检查对象是否为null来解决了这个问题,但现在我经常会收到以下异常:
android.database.sqlite.DatabaseObjectNotClosedException:未关闭游标或数据库对象
请帮助我处理此问题。
谢谢!
堆栈跟踪: 01-03 09:39:18.688: E/SQLiteDatabase(3063):在数据库'/data/data/com.opera.mini.android/databases/google_analytics.db'上没有显式调用close()方法 01-03 09:39:18.688: E/SQLiteDatabase(3063):android.database.sqlite.DatabaseObjectNotClosedException:应用程序没有关闭此处打开的游标或数据库对象 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1943) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:787) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.PersistentHitStore.loadExistingSession(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.PersistentHitStore.(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.PersistentHitStore.(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.google.android.apps.analytics.AnalyticsReceiver.onReceive(Unknown Source) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.opera.mini.android.OpMiniInstallReferrerReceiver.onReceive(Source:15) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.os.Handler.dispatchMessage(Handler.java:99) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.os.Looper.loop(Looper.java:137) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at android.app.ActivityThread.main(ActivityThread.java:4424) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at java.lang.reflect.Method.invokeNative(Native Method) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at java.lang.reflect.Method.invoke(Method.java:511) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-03 09:39:18.688: E/SQLiteDatabase(3063): at dalvik.system.NativeStart.main(Native Method) 01-03 09:39:18.688: E/System(3063): finalizer抛出未捕获的异常 01-03 09:39:18.688: E/System(306关闭连接的代码:
@Override
public void onTerminate() {
// TODO Auto-generated method stub
super.onTerminate();
snail_mail_db.close();
snail_mail_database_helper_obj.close();
}