我正在使用具有两种不同行布局的 ListView。数据已正确设置和显示,但是当我向上滚动时,它会出现空指针异常。在日志中它指向 holder.mainContainer 作为空指针,我在代码中添加了注释,我得到空指针异常。
public static class ViewHolder {
....
public TextView title;
public RelativeLayout mainContainer;
public LinearLayout categoryContainer;
public TextView submenuCategoryTitle;
....
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
final MultiTierMenu multiTierMenu = mMultiTierMenuList.get(position);
if (convertView == null) {
holder = new ViewHolder();
if(multiTierMenu instanceof SubMenu) {
vi = inflater.inflate(R.layout.event_list_snippet),
null);
....
holder.mainContainer = (RelativeLayout) vi.findViewById(R.id. snippet_container);
holder.title = (TextView) vi.findViewById(R.id.list_title);
....
vi.setTag(holder);
}
else if(multiTierMenu instanceof MenuCategory) {
vi = inflater.inflate(R.layout.submenu_category_list_item),
null);
....
holder.categoryContainer = (LinearLayout) vi.findViewById(R.id.category_container);
holder.submenuCategoryTitle = (TextView) vi.findViewById(R.id.subcategory_title);
....
vi.setTag(holder);
}
else
holder = (ViewHolder) vi.getTag();
//SubMenu
if(multiTierMenu instanceof SubMenu) {
final SubMenu subMenu = (SubMenu) multiTierMenu;
//GETTING NULL POINTER EXCEPTION HERE ON SCROLL UP
//if(null != holder.mainContainer) {
if(subMenu.getIsVisibleMenu())
==> holder.mainContainer.setVisibility(View.VISIBLE);
else
holder.mainContainer.setVisibility(View.GONE);
//}
//ADDING NULL CHECK ABOVE - GETTING NULL POINTER EXCEPTION HERE
holder.title.setText(subMenu.getTitle());
....
}
else if(multiTierMenu instanceof MenuCategory) {
final MenuCategory menuCategory = (MenuCategory) multiTierMenu;
holder.submenuCategoryTitle.setText(menuCategory.getTitle());
....
}
return vi;
}