Android application database crash

I am using android database and configured; but when i call it in onClickListener the app crashes.

the code I use

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 log cat to run i:

 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 noticed errors when the app opens, but I don't know where they come from.

When I take out the instructions to do with the database, there are no errors and everything works fine.

+3


source to share


3 answers


It won't help if the database is only open when the listener is set, it must be open when you use it. Call open()

and close()

Insert onCLick()

.



+1


source


I think you haven't opened and closed the database.

Before using the cursor

db.open();

      



and after using the cursor

db.close();

      

Try it.

+1


source


You need to use best practices for sqlite in android.

Sqlite cannot properly handle multiple connections (stream) even though they are properly synchronized. Use an assistant or content provider.

What are the best practices for SQLite on Android?

0


source







All Articles