我想在另一个方法中调用setAdapter(),但是在同一个类/活动上。但是,它给了我一个空指针异常。我有一个Listview lv = null;作为一个领域。
当setAdapter()在onCreate()中时,它确实有效:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new SemToGradeLoader(this).execute();
setContentView(R.layout.activity_grade_book);
lv = (ListView) findViewById(R.id.grade_list);
reloadListView();
CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item_grade, rowItems);
lv.setAdapter(adapter);
}
当setAdapter在reloadListView()中时,它不起作用:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new SemToGradeLoader(this).execute();
setContentView(R.layout.activity_grade_book);
lv = (ListView) findViewById(R.id.grade_list);
reloadListView();
}
这是我的reloadListView():
private void reloadListView() {
try {
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < subjects.length; i++) {
RowItem item = new RowItem(subjects[i], descriptions[i]);
rowItems.add(item);
}
CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item_grade, rowItems);
lv.setAdapter(adapter);
}
catch (IndexOutOfBoundsException e) {
}
答案 0 :(得分:0)
什么是:
new SemToGradeLoader(this).execute();
是异步任务还是什么?
从完成的方法开始,您是否尝试通过调用reloadListView()或其他东西来访问listview?