How to handle activity lifecycle to avoid NPE?
I have a `ListView` which adapts data from a cursor. The cursor's position of the record is stored via `View.setTag()`, so it can be retrieved in response to user events. 

    public class OrderActivity extends Activity {
    
      private ListView list;
      private CursorAdapter adapter;
      private SQLiteDatabase database;
    
      public void onResume() {
        database = new Database(this).getWriteableDatabase();
        // query the db and store the position of the cursor
        // as the tag while binding the view in the overridden
        // CursorAdapter.bindView()
        adapter = new SimpleCursorAdapter( /* code here */ );
        list.setAdapter(adapter);
      }
    
      public void onPause() {
        adapter.changeCursor(null);
        database.close();
      }
    
      private void onClickRowDumpOrder(View row) {
        int newPosition = (Integer) row.getTag();
        Cursor cursor = adapter.getCursor();
        int originalPosition = cursor.getPosition(); // Throws NPE
        
        cursor.moveToPosition(newPosition);
        Log.v("tag", "Description: " + cursor.getString(0));
        
        // restore the cursor
        cursor.moveToPosition(originalPosition);
      }
    
    }

I store the database and the adapter in instance fields of my Activity to allocate/free resources during the activity lifecycle: I create a new database `onResume` and close it `onPause`. Unfortunately, I receive lots of reports from my users that a NPE is thrown in the line outlined in the pseudocode above, but I'm not able to reproduce it.

It seems that `cursor` is `null`, but I wonder how this is possible if the method `onClickRowDumpOrder` can only be called after `onResume`, since it's the callback to the click event (I set this in the XML layout via `android:onClick`)

Am I doing something wrong? What API misusage causes the `cursor` to be null? Is there some documentation describing how cursors and adapters are intended to fit in the activity lifecycle?
How to handle activity lifecycle to avoid NPE?
===

I have a `ListView` which adapts data from a cursor. The cursor's position of the record is stored via `View.setTag()`, so it can be retrieved in response to user events. 

    public class OrderActivity extends Activity {
    
      private ListView list;
      private CursorAdapter adapter;
      private SQLiteDatabase database;
    
      public void onResume() {
        database = new Database(this).getWriteableDatabase();
        // query the db and store the position of the cursor
        // as the tag while binding the view in the overridden
        // CursorAdapter.bindView()
        adapter = new SimpleCursorAdapter( /* code here */ );
        list.setAdapter(adapter);
      }
    
      public void onPause() {
        adapter.changeCursor(null);
        database.close();
      }
    
      private void onClickRowDumpOrder(View row) {
        int newPosition = (Integer) row.getTag();
        Cursor cursor = adapter.getCursor();
        int originalPosition = cursor.getPosition(); // Throws NPE
        
        cursor.moveToPosition(newPosition);
        Log.v("tag", "Description: " + cursor.getString(0));
        
        // restore the cursor
        cursor.moveToPosition(originalPosition);
      }
    
    }

I store the database and the adapter in instance fields of my Activity to allocate/free resources during the activity lifecycle: I create a new database `onResume` and close it `onPause`. Unfortunately, I receive lots of reports from my users that a NPE is thrown in the line outlined in the pseudocode above, but I'm not able to reproduce it.

It seems that `cursor` is `null`, but I wonder how this is possible if the method `onClickRowDumpOrder` can only be called after `onResume`, since it's the callback to the click event (I set this in the XML layout via `android:onClick`)

Am I doing something wrong? What API misusage causes the `cursor` to be null? Is there some documentation describing how cursors and adapters are intended to fit in the activity lifecycle?