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.
On executing the last line (myList.setAdaper...) it bombs out with:
Can anyone please tell me what is happening to the cursor?
Code:
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:
Increasing code cache capacity to 512KB
D/ViewRootImpl@3afd46d[MainActivity]: ViewPostIme pointer 0
D/ViewRootImpl@3afd46d[MainActivity]: ViewPostIme pointer 1
D/ViewRootImpl@3afd46d[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/InputTransport: Input channel constructed: fd=73
D/ViewRootImpl@19a2dd7[OtherActivity]: setView = DecorView@3e736c4[OtherActivity] TM=true MM=false
D/ViewRootImpl@19a2dd7[OtherActivity]: dispatchAttachedToWindow
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/ViewRootImpl@19a2dd7[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/gralloc.msm8937.so from current namespace instead of sphal namespace.
D/OpenGLRenderer: eglCreateWindowSurface = 0x8bf64df0, 0x8b374008
D/ViewRootImpl@19a2dd7[OtherActivity]: MSG_RESIZED_REPORT: frame=Rect(456, 32 - 823, 799) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=2
MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@4ab74e2 nm : com.scorer.tennis 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/ViewRootImpl@19a2dd7[OtherActivity]: ViewPostIme pointer 0
D/ViewRootImpl@19a2dd7[OtherActivity]: ViewPostIme pointer 1
D/ViewRootImpl@19a2dd7[OtherActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/InputTransport: Input channel constructed: fd=75
D/ViewRootImpl@9bbc1c1[LogActivity]: setView = DecorView@6407d66[LogActivity] TM=true MM=false
D/ViewRootImpl@9bbc1c1[LogActivity]: dispatchAttachedToWindow
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.scorer.tennis, 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(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)
Can anyone please tell me what is happening to the cursor?