Probably by opening the db both in onCreate() and onResume() in some
circustances (e.g. first execution of your app) the db will be opened twice,
which for sure is not good.
If you open the db only in onResume() it should be fine (although I didn't
try it by myself). Usually, when I need data from the db, I call db.open(),
fetch my data, and db.close(). In this way I'm pretty sure there won't be
any such leak. Still I don't know if that's "the best way" to do it.


YuviDroid

On Thu, Jul 15, 2010 at 3:51 PM, Bender <abende...@googlemail.com> wrote:

> Hi,
>
> I'm writing an app which has 2 activities and gets his data from a
> database. Unfortunately I'm not sure when I have to open and close the
> database properly. In both activities I'm opening the database so the
> activities can access its data. When I do the following:
>
>  * Start the app
>  * Open activity 1
>  * Open activity 2 (via button in activity 1)
>  * Hit the back button (back to activity 1)
>  * Again back button (back to home screen)
>  * Start the app
>
> I'm receiving such an leak error:
>
> 07-15 14:34:19.504: ERROR/Database(234): Leak found
> 07-15 14:34:19.504: ERROR/Database(234):
> java.lang.IllegalStateException: mPrograms size 1
> 07-15 14:34:19.504: ERROR/Database(234):     at
> android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:
> 1669)
> 07-15 14:34:19.504: ERROR/Database(234):     at
> dalvik.system.NativeStart.run(Native Method)
> 07-15 14:34:19.504: ERROR/Database(234): Caused by:
> java.lang.IllegalStateException: /data/data/de.anote/databases/
> anote.db SQLiteDatabase created and never closed
>
> .... more stuff .....
>
> I guess it is because I'm not closing the database so implemented the
> following:
>
>  * db.open() in both onCreate() and both onResume() methods by the 2
> activities
>  * db.close() in both onPause() methods.
>
> But now I'm receiving another error:
>
> 07-15 15:27:18.472: ERROR/AndroidRuntime(266): Uncaught handler:
> thread main exiting due to uncaught exception
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):
> java.lang.RuntimeException: Unable to resume activity {de.anote/
> de.anote.gui.CategoryView}: java.lang.IllegalStateException: mQuery
> SELECT _id, note_name, value, date, category, priority, reminderbool,
> reminder, todo FROM t_note WHERE category=? ORDER BY date 1
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.app.ActivityThread.performResumeActivity(ActivityThread.java:
> 2950)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.app.ActivityThread.handleResumeActivity(ActivityThread.java:
> 2965)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1889)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.os.Looper.loop(Looper.java:123)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.app.ActivityThread.main(ActivityThread.java:4363)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> java.lang.reflect.Method.invokeNative(Native Method)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> java.lang.reflect.Method.invoke(Method.java:521)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> com.android.internal.os.ZygoteInit
> $MethodAndArgsCaller.run(ZygoteInit.java:860)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> dalvik.system.NativeStart.main(Native Method)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266): Caused by:
> java.lang.IllegalStateException: mQuery SELECT _id, note_name, value,
> date, category, priority, reminderbool, reminder, todo FROM t_note
> WHERE category=? ORDER BY date 1
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:162)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:536)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.app.Activity.performRestart(Activity.java:3736)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.app.Activity.performResume(Activity.java:3756)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.app.ActivityThread.performResumeActivity(ActivityThread.java:
> 2937)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     ... 10 more
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266): Caused by:
> android.database.sqlite.SQLiteMisuseException: library routine called
> out of sequence: handle 0x0
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.database.sqlite.SQLiteProgram.native_bind_string(Native
> Method)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:
> 178)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     at
> android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:153)
> 07-15 15:27:18.508: ERROR/AndroidRuntime(266):     ... 14 more
>
> Unfortunately I can't read which line in my code causes this exception
> but I guess it is because there is some point where is a database
> access and the db is still closed.
>
> So my question is, at which places should I open and close my
> database? It is right to open it in the onCreate(), onResume() methods
> and close it in onPause()?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Beginners" group.
>
> NEW! Try asking and tagging your question on Stack Overflow at
> http://stackoverflow.com/questions/tagged/android
>
> To unsubscribe from this group, send email to
> android-beginners+unsubscr...@googlegroups.com<android-beginners%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/android-beginners?hl=en
>



-- 
YuviDroid
http://android.yuvalsharon.net

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to