0

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

1 回答 1

2

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

  1. 获取项目视图类型

你必须按位置返回每个数字

@Override
public int getItemViewType(int position) {
    if (position % 2 == 0) {// example
        return 0;// MenuCategory
    }
    else {
        return 1;// SubMenu???
    }
}
  1. getViewTypeCount

您必须返回查看次数

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

如果实现了他们的方法,则仅在 (convertView == null) 中需要“膨胀”。喜欢跟...

@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;
    }
于 2013-01-25T12:35:28.783 回答