好吧,我迷路了并说'叔叔'。我正在尝试“重新打开已经关闭的对象:SQLiteQuery”错误消息 - 请参阅下面的日志。如您所见,粘贴在下面粘贴的方法中。它工作正常。有趣的是,我刚刚添加了一个cursor.close到一个完整的其他方法,因为我得到了一个'SQLite Android数据库游标窗口分配2048 kb失败'消息。
但我不确定为什么这种方法会引发错误? cursor.close始终位于同一位置,之前从未导致错误,并且显示正确。完全迷失了。
我看了下面这些帖子,但据我所知,我似乎正在做正确的事情。谢谢你的帮助!
Android - attempt to re-open an already-closed object: SQLiteQuery using loaderManager
attempt to reopen an already-closed object: sqlitequery
public List<dbData> queryToList(String myTable, String[] myColumns, String whereClause,
String[] whereArgs, String groupBy, String having, String orberBy) {
System.out.println("start queryToList");
List<dbData> dbDatas = new ArrayList<>();
Cursor cursor = database.query(myTable, myColumns,
whereClause, whereArgs, groupBy, having, orberBy);
System.out.println("cursor count = " + cursor.getCount());
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
dbData dbData = cursorToDB_Row(cursor, myTable);
dbData.setIntTemp1(0);
dbDatas.add(dbData);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return dbDatas;
}
日志:
03-21 21:51:44.695 9457-9457/com.mycompany.dudesmyreminders I/System.out﹕ start changeReminder
03-21 21:51:44.695 9457-9457/com.mycompany.dudesmyreminders I/System.out﹕ start Reminder
03-21 21:51:44.695 9457-9457/com.mycompany.dudesmyreminders I/System.out﹕ savePreference for reminder
03-21 21:51:44.695 9457-9457/com.mycompany.dudesmyreminders I/System.out﹕ start determineReminder
03-21 21:51:44.695 9457-9457/com.mycompany.dudesmyreminders I/System.out﹕ start relationshipSpace
03-21 21:51:44.695 9457-9457/com.mycompany.dudesmyreminders I/System.out﹕ start queryToList
03-21 21:51:44.705 9457-9457/com.mycompany.dudesmyreminders I/System.out﹕ cursor count = 110
03-21 21:51:44.705 9457-9457/com.mycompany.dudesmyreminders E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.dudesmyreminders, PID: 9457
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM reminders
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:178)
at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:150)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:214)
at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245)
at com.mycompany.dudesmyreminders.dbDataSource.queryToList(dbDataSource.java:147)
at com.mycompany.dudesmyreminders.Reminder.relationshipSpace(Reminder.java:104)
at com.mycompany.dudesmyreminders.Reminder.determineReminder(Reminder.java:83)
at com.mycompany.dudesmyreminders.MainActivity.changeReminder(MainActivity.java:144)
at com.mycompany.dudesmyreminders.MainActivity.onOptionsItemSelected(MainActivity.java:97)
at android.app.Activity.onMenuItemSelected(Activity.java:2907)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:556)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19858)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
编辑:已解决。在深夜,我看不出它是我添加cursor.close的方法之一,这导致了问题。该方法调用cursorToDB_Row。我不喜欢那里关闭光标。
//assumes ID is column 0 and the main string (usually name) is 1
private dbData cursorToDB_Row(Cursor cursor, String myTable) {
dbData dbData = new dbData();
dbData.setId(cursor.getLong(0)); //Id column
dbData.setDbData(cursor.getString(1)); //primary string column
dbData.setTable(myTable);
//cursor.close();
return dbData;
}
答案 0 :(得分:0)
解决。在深夜,我无法看到它是方法之一,我添加了cursor.close,这导致了问题。该方法调用cursorToDB_Row。它并不是那样我把光标关在那里。如下所示,我对此进行了评论,并且有效。
//assumes ID is column 0 and the main string (usually name) is 1
private dbData cursorToDB_Row(Cursor cursor, String myTable) {
dbData dbData = new dbData();
dbData.setId(cursor.getLong(0)); //Id column
dbData.setDbData(cursor.getString(1)); //primary string column
dbData.setTable(myTable);
//cursor.close();
return dbData;
}