我写了以下代码:
TodosOverviewActivity extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todo_list);
this.getListView().setDividerHeight(2);
registerForContextMenu(getListView());
fillData();
}
private void fillData() {
fetchEvents();
adapter2 = new MySimpleArrayAdapter
(this, nameOfEvent, startDates, descriptions);
setListAdapter(adapter2);
}
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.todo_list, parent, false);
TextView titleTextView = (TextView) rowView
.findViewById(R.id.todo_row_title);
TextView dateTextView = (TextView) rowView
.findViewById(R.id.todo_row_date);
ImageView imageView = (ImageView) rowView
.findViewById(R.id.todo_row_image);
titleTextView.setText(mTitles[position]);
..
}
然而,它在nullpointer titleTextView
上失败了。我错过了什么?
答案 0 :(得分:2)
更改此内容 - 在getView()
:
View rowView = inflater.inflate(R.layout.todo_list, parent, false);
为:
View rowView = inflater.inflate(R.layout.todo_row, parent, false);
为了您的代码,我希望fillData()
方法fetchEvents();
不会触发UI线程上的网络:)
答案 1 :(得分:1)
尝试:
View rowView = inflater.inflate(R.layout.todo_list, null);
并且不要忘记在函数末尾返回rowView
。