我在按下操作栏中的菜单时添加了一个 AlertDialog 以提示用户是否真的确定他想继续保存/删除我的 sqlite 中的项目列表,但我在下面收到此错误。我还尝试删除 AlertDialog ,而是在用户按下操作栏中的菜单以通知他们刚刚执行的操作后举杯,但我仍然在下面遇到相同的错误。如果我删除 AlertDialog 或 Toast 消息,一切都可以正常保存和删除我的 sqlite 中的项目。
09-22 10:26:09.865: E/AndroidRuntime(2341): FATAL EXCEPTION: main
09-22 10:26:09.865: E/AndroidRuntime(2341): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.usjr.sss/databases/subjecttaken
09-22 10:26:09.865: E/AndroidRuntime(2341): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
09-22 10:26:09.865: E/AndroidRuntime(2341): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1489)
09-22 10:26:09.865: E/AndroidRuntime(2341): at com.usjr.sss.adapter.SubjectTakenDbAdapter.deleteAllSubjects(SubjectTakenDbAdapter.java:56)
09-22 10:26:09.865: E/AndroidRuntime(2341): at com.usjr.sss.activity.CourseFragmentActivity$2.onClick(CourseFragmentActivity.java:143)
09-22 10:26:09.865: E/AndroidRuntime(2341): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
09-22 10:26:09.865: E/AndroidRuntime(2341): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 10:26:09.865: E/AndroidRuntime(2341): at android.os.Looper.loop(Looper.java:137)
09-22 10:26:09.865: E/AndroidRuntime(2341): at android.app.ActivityThread.main(ActivityThread.java:5227)
09-22 10:26:09.865: E/AndroidRuntime(2341): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:26:09.865: E/AndroidRuntime(2341): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:26:09.865: E/AndroidRuntime(2341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-22 10:26:09.865: E/AndroidRuntime(2341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-22 10:26:09.865: E/AndroidRuntime(2341): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
case R.id.action_save_subjects:
subjectTakenDbAdapter.open();
/**
* Delete from database all subjects taken if user does not select a
* subject & clicks save button
*/
if (arrayListSelectedSubject.isEmpty()) {
new AlertDialog.Builder(this).setMessage("Are you sure?")
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
subjectTakenDbAdapter.deleteAllSubjects();
}
}).show();
} else {
/**
* Add subjects taken to database
*/
for (int index = 0; index < arrayListSelectedSubject.size(); index++) {
subjectTakenDbAdapter
.createSubjectTaken((arrayListSelectedSubject
.get(index)));
Log.i("ADD SUBJECT TAKEN",
arrayListSelectedSubject.get(index));
}// end for loop
}// end if-else
Cursor cursor = subjectTakenDbAdapter.fetchAllSubjectTaken();
Log.i("SubjectTakenDbAdapter", String.valueOf(cursor.getCount()));
subjectTakenDbAdapter.close();
return true;
default:
return super.onOptionsItemSelected(item);
}// end switch case
}// end onOptionsItemSelected
logcat 告诉我这个错误的原因是指向我 CourseFragmentActivity.class 中的 java 行 143,这是这行代码:
subjectTakenDbAdapter.deleteAllSubjects();
我的 SubjectTakenDbAdapter.class 中的 java 第 56 行是这行代码:
return sqLiteDatabase.delete(DATABASE_TABLE, null, null) > 0;