can anyone see why I'm getting a Null Pointer exception on exit
Here is a short List activity that calls a custom adapter to show class room attendance. I set up the data using the classroom rowid that was clicked to get here and then query my DB to get all students in the class. I use a custom adapter because I have a list of checkboxes along with student names. No matter if I use my adapter or simply substitute in simplecursoradapter (and lose my checkbox functionality) upon exit I get a null pointer exception. This happens whether I click the "done" button or click the back arrow. I can't seem to see anything. Can you?
public class ShowStudentAttendance extends ListActivity {
private gradeBookDbAdapter mDbHelper;
private Long mRowId;
private TextView mNameText;
private String classname;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor stud;
mDbHelper = new gradeBookDbAdapter(this);
mDbHelper.open();
mRowId = (savedInstanceState == null) ? null
: (Long) savedInstanceState
.getSerializable(gradeBookDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? ex开发者_如何学Gotras
.getLong(gradeBookDbAdapter.KEY_ROWID) : null;
}
if (mRowId != null) {
stud = mDbHelper.fetchClass(mRowId);
startManagingCursor(stud);
classname = stud.getString(
stud.getColumnIndexOrThrow(gradeBookDbAdapter.KEY_CLASSNAME));
String title = "Attendance for " + classname;
setTitle(title);
}
stud = mDbHelper.fetchAllStudentsClass(mRowId);
startManagingCursor(stud);
setContentView(R.layout.attendance_list);
Button doneButton = (Button) findViewById(R.id.Done);
doneButton.setOnClickListener(mAttendanceActivity);
// Create an array to specify the fields we want to display in the list (only name)
String[] from = new String[]{gradeBookDbAdapter.KEY_NAME,
gradeBookDbAdapter.KEY_ROWID};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.stuname};
// Now create a simple cursor adapter and set it to display
MyDataAdapter studs =
new MyDataAdapter(this, R.layout.show_attendance, stud, from, to);
setListAdapter(studs);
}
private OnClickListener mAttendanceActivity = new OnClickListener() {
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
};
}
It seems you are not closing cursor or database befe exit activity.
Oops. Nevermind. I was looking in the wrong place. Nothing wrong with this code. I was just calling the activity with startActivityForResult and didn't need to and never had an onActivityResult(). I changed it to a simple startActivity and all is well again. Thanks for looking, though. –
精彩评论