带有两个不同布局的ListView在滚动时获取空指针异常

时间:2013-01-25 12:21:06

标签: android listview nullpointerexception

我正在使用具有两种不同行布局的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;
}

1 个答案:

答案 0 :(得分:2)

您是否实施了以下两种方法?

  1. getItemViewType
  2. 你必须按位置返回每个号码

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {// example
            return 0;// MenuCategory
        }
        else {
            return 1;// SubMenu???
        }
    }
    
    1. getViewTypeCount
    2. 您必须返回观看次数

      @Override
      public int getViewTypeCount() {
          return 2;
      }
      

      如果实现了他们的方法,只需要在(convertView == null)中使用“inflate”。 喜欢以下......

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder = null;
          int type = getItemViewType(position);
          if (convertView == null){
              holder = new ViewHolder();
              switch (type) {
                  case 0:// MenuCategory
                      convertView = inflater.inflate(R.layout.event_list_snippet, null);
                      holder.mainContainer = (RelativeLayout) convertView.findViewById(R.id. snippet_container);
                      holder.title = (TextView) convertView.findViewById(R.id.list_title);
                      break;
                  case 1:// SubMenu???
                      convertView = inflater.inflate(R.layout.submenu_category_list_item, null);
                      holder.categoryContainer = (LinearLayout) convertView.findViewById(R.id.category_container);
                      holder.submenuCategoryTitle = (TextView) convertView.findViewById(R.id.subcategory_title);
                      break;
              }
              convertView.setTag(holder);
          }
          else {
              holder = (ViewHolder) convertView.getTag();
          }
      
          // assign data into view
          switch (type) {
          case 0:// MenuCategory
              holder.submenuCategoryTitle.setText(menuCategory.getTitle());
              break;
          case 1:// SubMenu???
              holder.title.setText(subMenu.getTitle());
              break;
          }