My activity displays a ListView which retrieves its data from the 
associated cursor. I close the database (and the cursor) onPause, and get a 
new database (and a new cursor) onResume. This means that events callback 
should access the Cursor only when it's not null. However I get lots of NPE 
in my listeners. This is the pseudo code (I don't post the whole activity 
because it's 200 lines and is not runnable without also having XML files, 
strings and drawables)

onResume() {
  super.onResume();
  database = new Database(this).getWriteableDatabase();
  adapter = new SimpleCursorAdapter( /* query the SQLite db */);
  listView.setAdapter(adapter);
}

onPause() {
  super.onPause();
  adapter.changeCursor(null); // this closes the cursor
  database.close();
}

onClickItem(View view) {
  Cursor cursor = adapter.getCursor();
  int position = cursor.getPosition(); // This line throws the NPE
}


I can't understand why cursor may be null, since onResume() it's clearly 
initialized by the query. I can't reproduce this on my devices, which makes 
it really hard to debug. Also, the situation is quite unrecoverable, 
meaning that I would consider it a programming error to check inside the 
event callback if cursor is null, since obviously it must be non-null. If 
it is, it's better to let my app fail, so I eventually fix this.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to