列表视图有一点问题。它工作正常,有一个例外:当我向下滚动到列表视图的底部时,我得到一个空指针异常。使用调试器,我已经验证了 arraylist 的加载顺序完全正确,并且日期标签在它们应该在的位置。但是,一旦我开始滚动到必须看到新项目的位置,就会出现错误。我不确定为什么会发生这种情况,并且仅当列表滚动到足以使某些元素弹出而其他元素需要进入视野时。
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Object> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<Object> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public int getItemViewType(int position) {
Object item = getItem(position);
if(item instanceof Appointment) {
return 0;
} else {
// It's a section title:
return 1;
}
}
public View getView(int position, View convertView, ViewGroup parent)
{
Object item = getItem(position);
if(convertView == null)
{
// Create item view for first time
if(item instanceof Appointment)
{ // inflate appointment list view item layout
convertView = mInflater.inflate(R.layout.calendar_item, null);
}
else
{ // inflate title section list view item layout
convertView = mInflater.inflate(R.layout.calendar_item_header, null);
}
}
// Update list view item view according to type:
if(item instanceof Appointment)
{
Appointment a = (Appointment) item;
ViewHolder holder;
holder = new ViewHolder();
holder.txtAttendee = (TextView) convertView.findViewById(R.id.attendee);
holder.txtSummary = (TextView) convertView.findViewById(R.id.summary);
holder.txtStarts = (TextView) convertView.findViewById(R.id.starts);
holder.txtEnds = (TextView) convertView.findViewById(R.id.ends);
holder.txtCategories = (TextView) convertView.findViewById(R.id.categories);
convertView.setTag(holder);
convertView.setPadding(4, 4, 4, 16);
holder.txtAttendee.setText(a.GetAttendee()); // exception here
holder.txtSummary.setText(">" + a.GetSummary());
String st = a.GetDTStart().toString();
String en = a.GetDTEnd().toString();
holder.txtStarts.setText(st.substring(0,16));
holder.txtEnds.setText(en.substring(0,16));
String cat = a.GetCategories();
holder.txtCategories.setText(cat);
}
else if(item instanceof String)
{
// Item is a section header string:
String label = (String) item;
// Retrieve label TextView from convertView... etc...
HeaderHolder holder;
holder = new HeaderHolder();
holder.txtHeader = (TextView) convertView.findViewById(R.id.header);
convertView.setTag(holder);
convertView.setPadding(2,2,2,2);
convertView.setClickable(false);
convertView.setFocusable(false);
holder.txtHeader.setText(" " + label);
}
return convertView;
}
static class ViewHolder
{
TextView txtAttendee;
TextView txtSummary;
TextView txtStarts;
TextView txtEnds;
TextView txtCategories;
}
static class HeaderHolder
{
TextView txtHeader;
}
}
我在第 2414 行的“ViewRoot.deliverKeyEventToViewHierarchy”处得到一个空指针异常。
我究竟做错了什么?提前致谢!