我有一个显示高分表的 SQLite 数据库。如果用户愿意,我想为用户提供删除高分的选项(见下面的截图)。如果用户选择是,我希望对话框关闭,然后对话框后面的屏幕为空,除了 4 个列标题、2 个按钮和活动标题。
现在,当用户选择 Yes 时,它会给出下面的 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)
数据库助手.java
//Delete all rows/whole table from high scores.
public int deleteAll() {
return db.delete(TABLE, null, null); //Line 46
}
高分.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();
}
我的问题是如何让用户在程序不崩溃的情况下删除数据库。