开发者

error creating database in android

i am displaying the database in a listview in android. i am displaying the listview in another activity. i am creating database in oncreate by just calling a class called "data" that extends sqliteopenhelper and passing the table but as soon as i launch the app the stack trace in DDMS is

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.a开发者_Go百科pp.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

and my code is

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


As I don't have enough reputation to put a comment yet >.< I'm gonna put this as an answer instead.

The stack trace contains this SQLiteDatabase created and never closed which leads me to believe that the database was never closed. I'm assuming that Android is griping about the three lines here db = this.openOrCreateDatabase("location.db", SQLiteDatabase.OPEN_READWRITE, null); d = new data(this,Geo_Create_Table); db=d.getWritableDatabase(); and since thats all that you provided I think you think the same. If I understand what this is trying to do, you are opening a locally stored Database db and then, I guess d is also referencing the same locally stored Database. MAYBE (not too sure) that is causing the memory leaks in your app, but one thing I do know is that you need to add the db.close() in there somewhere.

You could try the suggestion that this other user made by using a try {} finally{db.close()} Need help with Database Access in Android


You need to close the the database connection before leaving an activity. The best way is to close on the onPause() callback and open on onResume(). This will ensure that the database connection is always in a valid state.

Relevant questions/answers here and here.

The second link shows a strategy for sharing a database connection across activities by extending the Application class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜