我在按下操作栏中的菜单时添加了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;
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句