在android中创建数据库时出错

时间:2011-08-23 09:41:19

标签: android sqlite

我在android中的listview中显示数据库。我在另一个活动中显示列表视图。我在oncreate中创建数据库只需调用一个名为“data”的类来扩展sqliteopenhelper并传递表,但是一旦我启动应用程序,DDMS中的堆栈跟踪就是

08-23 14:29:07.771: ERROR/Database(5175): Leak found
08-23 14:29:07.771: ERROR/Database(5175): java.lang.IllegalStateException: /data/data/com.cortes/databases/location.db SQLiteDatabase created and never closed
08-23 14:29:07.771: ERROR/Database(5175):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1792)
08-23 14:29:07.771: ERROR/Database(5175):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:798)
08-23 14:29:07.771: ERROR/Database(5175):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:857)
08-23 14:29:07.771: ERROR/Database(5175):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:850)
08-23 14:29:07.771: ERROR/Database(5175):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:539)
08-23 14:29:07.771: ERROR/Database(5175):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
08-23 14:29:07.771: ERROR/Database(5175):     at com.cortes.Cortes.onCreate(Cortes.java:79)
08-23 14:29:07.771: ERROR/Database(5175):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 14:29:07.771: ERROR/Database(5175):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-23 14:29:07.771: ERROR/Database(5175):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-23 14:29:07.771: ERROR/Database(5175):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-23 14:29:07.771: ERROR/Database(5175):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-23 14:29:07.771: ERROR/Database(5175):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 14:29:07.771: ERROR/Database(5175):     at android.os.Looper.loop(Looper.java:123)
08-23 14:29:07.771: ERROR/Database(5175):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-23 14:29:07.771: ERROR/Database(5175):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 14:29:07.771: ERROR/Database(5175):     at java.lang.reflect.Method.invoke(Method.java:521)
08-23 14:29:07.771: ERROR/Database(5175):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
08-23 14:29:07.771: ERROR/Database(5175):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
08-23 14:29:07.771: ERROR/Database(5175):     at dalvik.system.NativeStart.main(Native Method)
08-23 14:29:07.846: ERROR/wpa_supplicant(2300): wpa_driver_priv_driver_cmd failed
08-23 14:29:07.846: ERROR/wpa_supplicant(2300): wpa_driver_priv_driver_cmd failed

我的代码是

db = this.openOrCreateDatabase("location.db", SQLiteDatabase.OPEN_READWRITE, null);
d = new data(this,Geo_Create_Table);
db=d.getWritableDatabase();

2 个答案:

答案 0 :(得分:0)

由于我没有足够的声誉来发表评论&gt;。&lt;我会把它作为答案。

堆栈跟踪包含此 SQLiteDatabase已创建且从未关闭,这使我相信数据库从未关闭。我假设Android正在抓住这三条线     db = this.openOrCreateDatabase(“location.db”,SQLiteDatabase.OPEN_READWRITE,null);     d =新数据(this,Geo_Create_Table);     分贝= d.getWritableDatabase(); 因为那就是你提供的所有内容,我认为你的想法是一样的。如果我理解这是尝试做什么,那么你打开一个本地存储的数据库db然后,我猜d也引用了同一个本地存储的数据库。可能(不太确定)会导致应用程序中的内存泄漏,但我知道的一件事是你需要在某处添加db.close()

您可以尝试使用try {}最终{db.close()}建议其他用户 Need help with Database Access in Android

答案 1 :(得分:0)

您需要在离开活动之前关闭数据库连接。最好的方法是关闭onPause()回调并在onResume()上打开。这将确保数据库连接始终处于有效状态。

相关问题/答案herehere

第二个链接显示了通过扩展Application类来跨活动共享数据库连接的策略。