• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

ListView problem. Cursor gets closed.

mlgch1

Lurker
Sep 3, 2020
7
3
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.

Code:
package com.scorer.tennis;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class LogActivity extends Activity {

    DBAdapter myDb;

    // ***********************************

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);

        myDb = new DBAdapter(this);
        myDb.open();

        populateListViewFromDB();
    }

    // ***********************************

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myDb.close();
    }

    // ***********************************

    public void Clear_Log(View view) {
        myDb.Clear_Log();
        finish();
    }

    // ***********************************

    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.
        startManagingCursor(cursor);

        // 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[]{R.id.item_id, R.id.item_date, R.id.item_time, R.id.item_log};

        // 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(R.id.id_log_list);
        myList.setAdapter(myCursorAdapter);
    }
}
On executing the last line (myList.setAdaper...) it bombs out with:

Code:
W/Adreno-EGL: <qeglDrvAPI_eglGetConfigAttrib:612>: EGL_BAD_ATTRIBUTE
D/vndksupport: Loading /vendor/lib/hw/gralloc.msm8937.so from current namespace instead of sphal namespace.
D/OpenGLRenderer: eglCreateWindowSurface = 0x8c06fe68, 0x8b5ce008
D/ViewRootImpl@a5eda92[OtherActivity]: MSG_RESIZED_REPORT: frame=Rect(456, 32 - 823, 799) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=2
D/ViewRootImpl@a5eda92[OtherActivity]: MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@8c0dde nm : com.scorer.tennis ic=null
D/InputMethodManager: startInputInner - Id : 0
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=74
D/InputTransport: Input channel destroyed: fd=72
D/ViewRootImpl@a5eda92[OtherActivity]: ViewPostIme pointer 0
D/ViewRootImpl@a5eda92[OtherActivity]: ViewPostIme pointer 1
D/ViewRootImpl@a5eda92[OtherActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/InputTransport: Input channel constructed: fd=75
D/ViewRootImpl@a934784[LogActivity]: setView = DecorView@497db6d[LogActivity] TM=true MM=false
D/ViewRootImpl@a934784[LogActivity]: dispatchAttachedToWindow
D/AndroidRuntime: Shutting down VM
   
   
    --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.scorer.tennis, PID: 1109
    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(AbstractWindowedCursor.java:139)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:150)
        at android.widget.CursorAdapter.getView(CursorAdapter.java:289)
        at android.widget.AbsListView.obtainView(AbsListView.java:3180)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1448)
        at android.widget.ListView.onMeasure(ListView.java:1355)
        at android.view.View.measure(View.java:23407)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:909)
        at android.view.View.measure(View.java:23407)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2952)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1950)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2236)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1843)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7978)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

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

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones