0
我有一個顯示高分表的SQLite數據庫。我想給用戶選擇刪除高分(如果他們想要的話)(見下面的截圖)。如果用戶選擇是,我希望關閉對話框,除了4列標題,2個按鈕和活動頭之外,對話框後面的屏幕是空的。刪除SQLite表時出現IllegalStateException - Android
現在,當用戶選擇是時,它會給出下面的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();
}
我的問題是如何讓用戶刪除數據庫沒有程序崩潰。
可逆與拼寫。在運行對它的語句之前,您需要打開數據庫。 – 323go 2013-02-20 15:11:04
謝謝你的錯字修復。 :) – Matt 2013-02-20 15:37:59