我有一个问题,我现在正在努力解决几天。它一直给我一个错误,即我的数据库没有关闭。我从我的 sql 数据库中取出项目并将它们放在带有自定义适配器的 gridview 中。如果您单击一个项目,它将带您到另一个类(answer.java)。什么都不做并点击后退按钮不会导致错误,但是如果我调用某个函数并在此函数之后点击后退按钮,我会在按下后退按钮时收到 close() 错误。所以这是确切的错误:
11-19 19:56:22.485: E/Database(7403): close() was never explicitly called on database '/data/data/com.test.game/databases/quizDb'
11-19 19:56:22.485: E/Database(7403): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
11-19 19:56:22.485: E/Database(7403): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
11-19 19:56:22.485: E/Database(7403): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
11-19 19:56:22.485: E/Database(7403): at com.test.game.db.DataBaseHelper.openDataBase(DataBaseHelper.java:117)
11-19 19:56:22.485: E/Database(7403): at com.test.game.Answer.loadDataBase(Answer.java:179)
11-19 19:56:22.485: E/Database(7403): at com.test.game.Answer.onClick(Answer.java:240)
11-19 19:56:22.485: E/Database(7403): at android.view.View.performClick(View.java:2491)
11-19 19:56:22.485: E/Database(7403): at android.view.View$PerformClick.run(View.java:9086)
11-19 19:56:22.485: E/Database(7403): at android.os.Handler.handleCallback(Handler.java:587)
11-19 19:56:22.485: E/Database(7403): at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 19:56:22.485: E/Database(7403): at android.os.Looper.loop(Looper.java:130)
11-19 19:56:22.485: E/Database(7403): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 19:56:22.485: E/Database(7403): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 19:56:22.485: E/Database(7403): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 19:56:22.485: E/Database(7403): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
11-19 19:56:22.485: E/Database(7403): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
11-19 19:56:22.485: E/Database(7403): at dalvik.system.NativeStart.main(Native Method)
我想我找到了导致此错误的代码部分,如果我不再在 answer.java 文件中调用 myDbHelper.insertString(...) ,我就不会再收到错误了...
public void insertString(int id, int level, String field, String player) {
Cursor c = myDataBase.rawQuery(
"SELECT * FROM ANSWERS WHERE QUESTION_ID=" + id
+ " AND PLAYER ='" + player + "'", null);
myDataBase = this.getWritableDatabase();
if (c != null) {
myDataBase.execSQL("UPDATE ANSWERS SET " + field + "=1"
+ " WHERE QUESTION_ID=" + id + " AND LEVEL =" + level
+ " AND PLAYER='" + player + "'");
} else {
myDataBase
.execSQL("INSERT INTO ANSWERS (level, player, question_id, "
+ field + ") VALUES ("+ level + ",'" + player + "'," + id + ", 1)");
}
c.close();
if (myDataBase != null) {
myDataBase.close();
}
}
我尝试但未解决的一件事是将其添加到我的 answer.java 文件中:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (myDbHelper != null) {
myDbHelper.close();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
// 我的 databasehelper.java 中的 .close() 是:
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
if (c != null)
c.close();
super.close();
}
希望有人能给我指点灯!
编辑
@sam 你的解决方案给了我这个:
11-19 19:52:42.735: E/AndroidRuntime(6982): FATAL EXCEPTION: main
11-19 19:52:42.735: E/AndroidRuntime(6982): android.database.sqlite.SQLiteException: attempt to write a readonly database: UPDATE ANSWERS SET showhint1=1 WHERE QUESTION_ID=18 AND LEVEL =1 AND PLAYER='trst'
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
11-19 19:52:42.735: E/AndroidRuntime(6982): at com.test.game.db.DataBaseHelper.insertString(DataBaseHelper.java:301)
11-19 19:52:42.735: E/AndroidRuntime(6982): at com.test.game.Answer.onClick(Answer.java:241)
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.view.View.performClick(View.java:2491)
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.view.View$PerformClick.run(View.java:9086)
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.os.Handler.handleCallback(Handler.java:587)
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.os.Looper.loop(Looper.java:130)
11-19 19:52:42.735: E/AndroidRuntime(6982): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 19:52:42.735: E/AndroidRuntime(6982): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 19:52:42.735: E/AndroidRuntime(6982): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 19:52:42.735: E/AndroidRuntime(6982): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
11-19 19:52:42.735: E/AndroidRuntime(6982): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
11-19 19:52:42.735: E/AndroidRuntime(6982): at dalvik.system.NativeStart.main(Native Method)