Finalizing a Cursor that has not been deactivated or closed non-fatal error

前端 未结 9 1463
慢半拍i
慢半拍i 2020-12-07 19:27

i\'m getting a \"Finalizing a Cursor that has not been deactivated or closed\" error on this piece of code. The code is used to fill a listview.

Since it\'s a non

相关标签:
9条回答
  • 2020-12-07 19:48

    Do not use startManagingCursor() since that is no longer the recommended approach. The issue occurs because a cursor / DB connection is still not closed by the time the finalizer gets to this object. You can avoid that by either allowing a loader to manage the cursor or by tracking all cursor / DB / SQLiteOpenHelper connections yourself and cleaning up after them.

    Using a Loader is fairly cumbersome and requires a lot of moving parts for it to work in conjunction with say a list view. On the other hand tracking your cursor and database connections is prone to human error. If the number of cursor / DB objects is low, I'd recommend the latter solution. If not, let a loader handle your connections.

    0 讨论(0)
  • 2020-12-07 19:50

    I struggled with this problem for two days. I was trying to get sample code working which passed a cursor, returned from a database query, directly to List Adapter - no interim adapter. It refused to work - just displayed a blank screen - until I invoked 'moveToFirst()' on the cursor before passing it to the ListAdapter. Go figure! When I comment this out, it breaks.

    Just thought that I'd share this to save people the same struggles that I had.

    If anyone can shed some light on why this is so, I'd appreciate it. I haven't had to invoke moveToFirst on cursors up to now, to get them to perform properly.

    0 讨论(0)
  • 2020-12-07 19:53

    When a query returns a cursor it is actually positioned "before" the first record in the cursor. An adapter will attempt do a 'getItem' at the first element so it will fail as the cursor is not positioned at any.

    In my base adapters I do a cursorMoveToPosition on the getViews. This seems to eliminate the need for the movefirst.

    0 讨论(0)
提交回复
热议问题