滚动游标支持的列表视图时崩溃

Crash while scrolling a cursor backed listview

正如标题所说,我有一个列表视图,其中填充了一个游标适配器。比方说,我可以很好地滚动列表的前 50 个元素,但是如果我尝试进一步滚动它会崩溃,并且它总是在同一点崩溃。我包含了堆栈跟踪,但它对我来说并不是很有用:所有调用都是在 android 框架内进行的,所以即使我知道问题最终是一个关闭的数据库,我也不知道不知道我的代码的哪一部分有问题。在同一个应用程序中,我有其他基于游标的列表视图,它们不会造成任何问题。我对原因没有任何其他想法。

java.lang.IllegalStateException: The database '/storage/emulated/0/***/db/***.sqlite' is not open.
        at android.database.sqlite.SQLiteDatabase.throwIfNotOpenLocked(SQLiteDatabase.java:2169)
        at android.database.sqlite.SQLiteDatabase.createSession(SQLiteDatabase.java:365)
        at android.database.sqlite.SQLiteDatabase.initialValue(SQLiteDatabase.java:84)
        at android.database.sqlite.SQLiteDatabase.initialValue(SQLiteDatabase.java:81)
        at java.lang.ThreadLocal$Values.getAfterMiss(ThreadLocal.java:430)
        at java.lang.ThreadLocal.get(ThreadLocal.java:65)
        at android.database.sqlite.SQLiteDatabase.getThreadSession(SQLiteDatabase.java:359)
        at android.database.sqlite.SQLiteProgram.getSession(SQLiteProgram.java:101)
        at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
        at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:152)
        at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:124)
        at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:214)
        at android.database.CursorWrapper.moveToPosition(CursorWrapper.java:162)
        at android.widget.CursorAdapter.getItemId(CursorAdapter.java:223)
        at android.widget.AbsListView$RecycleBin.retrieveFromScrap(AbsListView.java:6753)
        at android.widget.AbsListView$RecycleBin.getScrapView(AbsListView.java:6492)
        at android.widget.AbsListView.obtainView(AbsListView.java:2343)
        at android.widget.ListView.makeAndAddView(ListView.java:1864)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillGap(ListView.java:662)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4968)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4512)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:549)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

您已经关闭了您的数据库,并且 SQLiteCursor 需要在滚动 ListView 时填充它的 window,请参阅此处 https://groups.google.com/forum/#!msg/android-developers/NwDRpHUXt0U/jIam4Q8-cqQJ 关于何时应该关闭您的数据库,答案是:不需要这样做