I have a SQLite db that displays a high scores table. I want to give the user the option to delete the high scores if they want (see screenshot below). If the user selects Yes, I want the dialog to close and then the screen behind the dialog to be empty except for the 4 column headers, 2 buttons and the activity header.
Right now, when the user selects Yes it gives the below LogCat.
02-20 09:59:39.686: E/AndroidRuntime(2458): FATAL EXCEPTION: main
02-20 09:59:39.686: E/AndroidRuntime(2458): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/matt.lyons.bibletrivia/databases/test3
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1489)
02-20 09:59:39.686: E/AndroidRuntime(2458): at matt.lyons.bibletrivia.DatabaseHelper.deleteAll(DatabaseHelper.java:46)
02-20 09:59:39.686: E/AndroidRuntime(2458): at matt.lyons.bibletrivia.Highscores$3.onClick(Highscores.java:198)
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.view.View.performClick(View.java:4202)
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.view.View$PerformClick.run(View.java:17340)
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.os.Handler.handleCallback(Handler.java:725)
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.os.Looper.loop(Looper.java:137)
02-20 09:59:39.686: E/AndroidRuntime(2458): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-20 09:59:39.686: E/AndroidRuntime(2458): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 09:59:39.686: E/AndroidRuntime(2458): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 09:59:39.686: E/AndroidRuntime(2458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-20 09:59:39.686: E/AndroidRuntime(2458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-20 09:59:39.686: E/AndroidRuntime(2458): at dalvik.system.NativeStart.main(Native Method)
DatabaseHelper.java
//Delete all rows/whole table from high scores.
public int deleteAll() {
return db.delete(TABLE, null, null); //Line 46
}
Highscores.java
public void areYouSure() {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.clearhighscores);
dialog.setTitle("Clear Highscores?");
TextView question = (TextView)dialog.findViewById(R.id.question);
Button yes = (Button)dialog.findViewById(R.id.yes);
Button no = (Button)dialog.findViewById(R.id.no);
question.setText("This is not reversable. Are you sure you want to delete the high scores?");
yes.setText("Yes");
no.setText("No");
yes.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dh.deleteAll(); //Line 198
dialog.dismiss();
}
});
no.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}

My question is how to let the user delete the db without the program crashing.
It means that you trying to delete data but your database is closed. So before delete you need to check if db is open and if not, open it else just perform delete operation.
Where you closing db? You should close any datasources, cursor etc. in
onDestroy()oronStop()method.