ListView problem. Cursor gets closed

Software Development and Hacking
I am using ListView to display a small list. I know it’s on the main thread, but this is not a problem. It used to run under earlier API levels but gives a problem under 27.


    private void populateListViewFromDB() {

        Cursor cursor = myDb.getAllLogRows();

        // Allow activity to manage lifetime of the cursor.
        // DEPRECATED! Runs on the UI thread, OK for small/short queries.

        // Setup mapping from cursor to view fields:
        String[] fromFieldNames = new String[]{DBAdapter.KEY_LOG_ID, DBAdapter.KEY_LOG_DATE, DBAdapter.KEY_LOG_TIME, DBAdapter.KEY_LOG_LOG};

        int[] toViewIDs = new int[]{,,,};

        // Create adapter to many columns of the DB onto elements in the UI.
        SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,    // Context
                R.layout.log_item_layout,                                              // Row layout template
                cursor,                                                                // cursor (set of DB records to map)
                fromFieldNames,                                                        // DB Column names
                toViewIDs);                                                            // View IDs to put information in

        // Set the adapter for the list view
        ListView myList = (ListView) findViewById(;

On executing the last line (myList.setAdaper…) it bombs out with:


    Increasing code cache capacity to 512KB
D/[email protected][MainActivity]: ViewPostIme pointer 0
D/[email protected][MainActivity]: ViewPostIme pointer 1
D/[email protected][MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/InputTransport: Input channel constructed: fd=73
D/[email protected][OtherActivity]: setView = [email protected][OtherActivity] TM=true MM=false
D/[email protected][OtherActivity]: dispatchAttachedToWindow
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/[email protected][OtherActivity]: Relayout returned: old=[0,0][0,0] new=[456,32][823,799] result=0x7 surface={valid=true 2335653888} changed=true
W/Adreno-EGL: <qeglDrvAPI_eglGetConfigAttrib:612>: EGL_BAD_ATTRIBUTE
D/vndksupport: Loading /vendor/lib/hw/ from current namespace instead of sphal namespace.
D/OpenGLRenderer: eglCreateWindowSurface = 0x8bf64df0, 0x8b374008
D/[email protected][OtherActivity]: MSG_RESIZED_REPORT: frame=Rect(456, 32 - 823, 799) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=2
V/InputMethodManager: Starting input: [email protected] nm : ic=null
D/InputMethodManager: startInputInner - Id : 0
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=74
    Input channel destroyed: fd=72
D/[email protected][OtherActivity]: ViewPostIme pointer 0
D/[email protected][OtherActivity]: ViewPostIme pointer 1
D/[email protected][OtherActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/InputTransport: Input channel constructed: fd=75
D/[email protected][LogActivity]: setView = [email protected][LogActivity] TM=true MM=false
D/[email protected][LogActivity]: dispatchAttachedToWindow
D/AndroidRuntime: Shutting down VM
    --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
    Process:, PID: 7020
    android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
        at android.database.AbstractWindowedCursor.checkPosition(
        at android.database.AbstractWindowedCursor.getString(
        at android.widget.SimpleCursorAdapter.bindView(
        at android.widget.CursorAdapter.getView(
        at android.widget.AbsListView.obtainView(
        at android.widget.ListView.measureHeightOfChildren(
        at android.widget.ListView.onMeasure(
        at android.view.View.measure(
        at android.widget.RelativeLayout.measureChildHorizontal(

Can anyone please tell me what is happening to the cursor?


Sharing is caring!

Leave a Reply