3

i am using a Android database and it's set up; but when i call it within an onClickListener the app crashes.

the code i am using is

db.open();
mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
               s = WorkoutChoice.this.weight.getText().toString();
               s2 = WorkoutChoice.this.height.getText().toString();
               int w = Integer.parseInt(s);
               double h = Double.parseDouble(s2);
               double BMI = (w/h)/h;
               t.setText(""+BMI);
               long id = db.insertTitle("001", ""+days, ""+BMI);
               Cursor c = db.getAllTitles();
               if (c.moveToFirst())
               {
                   do {          
                       DisplayTitle(c);
                   } while (c.moveToNext());
               }
            }
        });
        db.close();

and the log cat for when i run it is:

 04-01 18:21:54.704: E/global(6333): Deprecated Thread methods are not supported.
04-01 18:21:54.704: E/global(6333): java.lang.UnsupportedOperationException
04-01 18:21:54.704: E/global(6333):     at java.lang.VMThread.stop(VMThread.java:85)
04-01 18:21:54.704: E/global(6333):     at java.lang.Thread.stop(Thread.java:1391)
04-01 18:21:54.704: E/global(6333):     at java.lang.Thread.stop(Thread.java:1356)
04-01 18:21:54.704: E/global(6333):     at com.b00348312.workout.Splashscreen$1.run(Splashscreen.java:42)
04-01 18:22:09.444: D/dalvikvm(6333): GC_FOR_MALLOC freed 4221 objects / 252640 bytes in 31ms
04-01 18:22:09.474: I/dalvikvm(6333): Total arena pages for JIT: 11
04-01 18:22:09.574: D/dalvikvm(6333): GC_FOR_MALLOC freed 1304 objects / 302920 bytes in 29ms
04-01 18:22:09.744: D/dalvikvm(6333): GC_FOR_MALLOC freed 2480 objects / 290848 bytes in 33ms
04-01 18:22:10.034: D/dalvikvm(6333): GC_FOR_MALLOC freed 6334 objects / 374152 bytes in 36ms
04-01 18:22:14.344: D/AndroidRuntime(6333): Shutting down VM
04-01 18:22:14.344: W/dalvikvm(6333): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
04-01 18:22:14.364: E/AndroidRuntime(6333): FATAL EXCEPTION: main
04-01 18:22:14.364: E/AndroidRuntime(6333): java.lang.IllegalStateException: database not open
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1567)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1484)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at com.b00348312.workout.DataBaseHelper.insertTitle(DataBaseHelper.java:84)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at com.b00348312.workout.WorkoutChoice$3.onClick(WorkoutChoice.java:84)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.view.View.performClick(View.java:2408)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.view.View$PerformClick.run(View.java:8817)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.os.Handler.handleCallback(Handler.java:587)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.os.Looper.loop(Looper.java:144)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at java.lang.reflect.Method.invoke(Method.java:521)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-01 18:22:14.364: E/AndroidRuntime(6333):     at dalvik.system.NativeStart.main(Native Method)

I have noticed errors when the application opens but i don't know where they are from.

When I take out the statements to do with the database there are no errors and everthing runs smoothly.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
Darren Murtagh
  • 591
  • 2
  • 6
  • 28

3 Answers3

1

I think you haven't Opened and Closed the Databaase.

Before use of Cursor

db.open();

and after use of Cursor

db.close();

Try this thing.

Bhavin
  • 6,020
  • 4
  • 24
  • 26
  • i do have a db.open(); and db.Close(); before and after. i'll edit the post to indicate that – Darren Murtagh Apr 01 '12 at 17:35
  • @DarrenMurtagh you have but you don't open / close it in the onClick part which is asynchronously called at some point in the future so the db.close already happened. – zapl Apr 01 '12 at 17:38
  • @DarrenMurtagh: Just Put it Inside the not outside, Because when you click the Button then only you want to open the Database. – Bhavin Apr 01 '12 at 17:39
1

It won't help if the database is only open while you set the listener, it has to be open when you use it. Call open() and close() INSIDE the onCLick().

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
0

You need to use the best practices for sqlite in android.

Sqlite can not properly handle multiple connections (thread), even if they are properly synchronized. Use a helper, or a content provider.

What are the best practices for SQLite on Android?

Community
  • 1
  • 1
Guillermo Tobar
  • 344
  • 4
  • 17