Android 有人知道为什么我在退出时出现空指针异常吗
下面是一个短列表活动,它调用自定义适配器以显示教室出勤情况。 我使用单击此处获取的教室rowid设置数据,然后查询我的 DB让全班学生都参加。我使用自定义适配器,因为我有一个复选框列表 还有学生的名字。无论我是使用适配器还是简单地替换simplecursoradapter并在退出时丢失复选框功能,我都会得到一个空指针异常。这种情况是否会发生 我单击“完成”按钮或单击后退箭头。我好像什么也看不见。你能吗Android 有人知道为什么我在退出时出现空指针异常吗,android,Android,下面是一个短列表活动,它调用自定义适配器以显示教室出勤情况。 我使用单击此处获取的教室rowid设置数据,然后查询我的 DB让全班学生都参加。我使用自定义适配器,因为我有一个复选框列表 还有学生的名字。无论我是使用适配器还是简单地替换simplecursoradapter并在退出时丢失复选框功能,我都会得到一个空指针异常。这种情况是否会发生 我单击“完成”按钮或单击后退箭头。我好像什么也看不见。你能吗 public class ShowStudentAttendance extends List
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 ? extras
.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();
}
};
}
似乎您没有在退出活动之前关闭光标或数据库。Oops。没有关系。我找错地方了。这个代码没有问题。我刚刚用startActivityForResult调用了活动,不需要,也从来没有过onActivityResult。我把它改成了一个简单的星际触觉,一切又好了。不过,谢谢您的关注 哎呀。没有关系。我找错地方了。这个代码没有问题。我刚刚用startActivityForResult调用了活动,不需要,也从来没有过onActivityResult。我把它改成了一个简单的星际触觉,一切又好了。不过谢谢你的关注。