我在操作栏中按下菜单时添加了一个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;
答案 0 :(得分:1)
即使对话框打开,此onOptionsItemSelected
中的操作也将完全执行到最后。因此,它会调用subjectTakenDbAdapter.close();
。再次打开适配器:
new AlertDialog.Builder(this).setMessage("Are you sure?")
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
subjectTakenDbAdapter.open();
subjectTakenDbAdapter.deleteAllSubjects();
subjectTakenDbAdapter.close();
}
}).show();