1. Are you ready for the Galaxy S20? Here is everything we know so far!

ListView problem. Cursor gets closed.

Discussion in 'Android Development' started by mlgch1, Sep 3, 2020.

  1. mlgch1

    mlgch1 Lurker
    Thread Starter

    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 (Text):
    1.  
    2. package com.scorer.tennis;
    3.  
    4. import android.app.Activity;
    5. import android.database.Cursor;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.widget.ListView;
    9. import android.widget.SimpleCursorAdapter;
    10.  
    11. public class LogActivity extends Activity {
    12.  
    13.     DBAdapter myDb;
    14.  
    15.     // ***********************************
    16.  
    17.     @Override
    18.     protected void onCreate(Bundle savedInstanceState) {
    19.         super.onCreate(savedInstanceState);
    20.         setContentView(R.layout.activity_log);
    21.  
    22.         myDb = new DBAdapter(this);
    23.         myDb.open();
    24.  
    25.         populateListViewFromDB();
    26.     }
    27.  
    28.     // ***********************************
    29.  
    30.     @Override
    31.     protected void onDestroy() {
    32.         super.onDestroy();
    33.         myDb.close();
    34.     }
    35.  
    36.     // ***********************************
    37.  
    38.     public void Clear_Log(View view) {
    39.         myDb.Clear_Log();
    40.         finish();
    41.     }
    42.  
    43.     // ***********************************
    44.  
    45.     private void populateListViewFromDB() {
    46.  
    47.         Cursor cursor = myDb.getAllLogRows();
    48.  
    49.         // Allow activity to manage lifetime of the cursor.
    50.         // DEPRECATED! Runs on the UI thread, OK for small/short queries.
    51.         startManagingCursor(cursor);
    52.  
    53.         // Setup mapping from cursor to view fields:
    54.         String[] fromFieldNames = new String[]{DBAdapter.KEY_LOG_ID, DBAdapter.KEY_LOG_DATE, DBAdapter.KEY_LOG_TIME, DBAdapter.KEY_LOG_LOG};
    55.  
    56.         int[] toViewIDs = new int[]{R.id.item_id, R.id.item_date, R.id.item_time, R.id.item_log};
    57.  
    58.         // Create adapter to many columns of the DB onto elements in the UI.
    59.         SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,     // Context
    60.                 R.layout.log_item_layout,                                               // Row layout template
    61.                 cursor,                                                                 // cursor (set of DB records to map)
    62.                 fromFieldNames,                                                         // DB Column names
    63.                 toViewIDs);                                                             // View IDs to put information in
    64.  
    65.         // Set the adapter for the list view
    66.         ListView myList = (ListView) findViewById(R.id.id_log_list);
    67.         myList.setAdapter(myCursorAdapter);
    68.     }
    69. }
    70.  
    On executing the last line (myList.setAdaper...) it bombs out with:

    Code (Text):
    1.  
    2. W/Adreno-EGL: <qeglDrvAPI_eglGetConfigAttrib:612>: EGL_BAD_ATTRIBUTE
    3. D/vndksupport: Loading /vendor/lib/hw/gralloc.msm8937.so from current namespace instead of sphal namespace.
    4. D/OpenGLRenderer: eglCreateWindowSurface = 0x8c06fe68, 0x8b5ce008
    5. 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
    6. D/ViewRootImpl@a5eda92[OtherActivity]: MSG_WINDOW_FOCUS_CHANGED 1
    7. V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@8c0dde nm : com.scorer.tennis ic=null
    8. D/InputMethodManager: startInputInner - Id : 0
    9. I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
    10. D/InputTransport: Input channel constructed: fd=74
    11. D/InputTransport: Input channel destroyed: fd=72
    12. D/ViewRootImpl@a5eda92[OtherActivity]: ViewPostIme pointer 0
    13. D/ViewRootImpl@a5eda92[OtherActivity]: ViewPostIme pointer 1
    14. D/ViewRootImpl@a5eda92[OtherActivity]: MSG_WINDOW_FOCUS_CHANGED 0
    15. D/InputTransport: Input channel constructed: fd=75
    16. D/ViewRootImpl@a934784[LogActivity]: setView = DecorView@497db6d[LogActivity] TM=true MM=false
    17. D/ViewRootImpl@a934784[LogActivity]: dispatchAttachedToWindow
    18. D/AndroidRuntime: Shutting down VM
    19.    
    20.    
    21.     --------- beginning of crash
    22. E/AndroidRuntime: FATAL EXCEPTION: main
    23.     Process: com.scorer.tennis, PID: 1109
    24.     android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
    25.         at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:139)
    26.         at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
    27.         at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:150)
    28.         at android.widget.CursorAdapter.getView(CursorAdapter.java:289)
    29.         at android.widget.AbsListView.obtainView(AbsListView.java:3180)
    30.         at android.widget.ListView.measureHeightOfChildren(ListView.java:1448)
    31.         at android.widget.ListView.onMeasure(ListView.java:1355)
    32.         at android.view.View.measure(View.java:23407)
    33.         at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    34.         at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    35.         at android.view.View.measure(View.java:23407)
    36.         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
    37.         at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    38.         at android.view.View.measure(View.java:23407)
    39.         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
    40.         at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    41.         at android.view.View.measure(View.java:23407)
    42.         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
    43.         at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
    44.         at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
    45.         at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
    46.         at android.view.View.measure(View.java:23407)
    47.         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6967)
    48.         at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    49.         at com.android.internal.policy.DecorView.onMeasure(DecorView.java:909)
    50.         at android.view.View.measure(View.java:23407)
    51.         at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2952)
    52.         at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1950)
    53.         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2236)
    54.         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1843)
    55.         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7978)
    56.         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    57.         at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    58.         at android.view.Choreographer.doFrame(Choreographer.java:658)
    59.         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    60.         at android.os.Handler.handleCallback(Handler.java:790)
    61.         at android.os.Handler.dispatchMessage(Handler.java:99)
    62.         at android.os.Looper.loop(Looper.java:164)
    63.         at android.app.ActivityThread.main(ActivityThread.java:7000)
    64.         at java.lang.reflect.Method.invoke(Native Method)
    65.         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    66.         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
    67.  
    Can anyone please tell me what is happening to the cursor?
     


Loading...
Similar Threads - ListView problem Cursor
  1. Eddy155
    Replies:
    0
    Views:
    117
  2. mike WRIGHT
    Replies:
    2
    Views:
    187
  3. faran100
    Replies:
    0
    Views:
    428
  4. ElChief
    Replies:
    1
    Views:
    258
  5. Ponnangan
    Replies:
    0
    Views:
    454
  6. LefmyHans
    Replies:
    0
    Views:
    321
  7. Nightpoison
    Replies:
    1
    Views:
    375
  8. maneeshalampalli
    Replies:
    2
    Views:
    346
  9. maneeshalampalli
    Replies:
    4
    Views:
    1,003
  10. Mingi
    Replies:
    0
    Views:
    313

Share This Page

Loading...