在Android 2.3中选择项目时,ExpandableListView应用程序的导航抽屉会崩溃

时间:2015-04-16 12:07:48

标签: android expandablelistview android-2.3-gingerbread expandablelistadapter

我有一个带有ExpandableListView的NavigationDrawer,其中只有一些项目有子项目。另外,列表的第一项具有与ExpandableListView的其他组项不同的布局,布局仅具有imageview。该应用程序适用于所有设备,但在Android 2.3应用程序崩溃的设备。这是ExpandableListView的适配器:

public class NavDrawerAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<NavDrawerItem> _listDataHeader; // header titles
private HashMap<String, List<NavMenuSection>> _listDataChild;

private static class NavMenuItemHolder {
    private TextView labelView;
    private ImageView iconView;
}

private class NavMenuSectionHolder {
    private TextView labelView;
}

private class NavMenuHeaderHolder {
    private ImageView imgView;
}

public NavDrawerAdapter(Context context,
        List<NavDrawerItem> listDataHeader,
        HashMap<String, List<NavMenuSection>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(
            this._listDataHeader.get(groupPosition).getLabel()).get(
            childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition,
        final int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {

    NavMenuSection child = (NavMenuSection) getChild(groupPosition,
            childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.navdrawer_section,
                null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.navmenusection_label);

    txtListChild.setText(child.getLabel());
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {

    if (this._listDataChild.get(
            this._listDataHeader.get(groupPosition).getLabel())!= null)
        return this._listDataChild.get(
                this._listDataHeader.get(groupPosition).getLabel()).size();
    else 
        return 0;
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    NavDrawerItem menuItem = this._listDataHeader.get(groupPosition);

    if ( menuItem.getType() == NavMenuItem.ITEM_TYPE ) {
        convertView = getItemView(convertView, parent, menuItem );
    } else if (menuItem.getType() == NavMenuHeader.HEADER_TYPE){
        convertView = getHeaderView(convertView, parent, menuItem);
    } 

    return convertView;
}

public View getItemView( View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem ) {

    NavMenuItem menuItem = (NavMenuItem) navDrawerItem ;
    NavMenuItemHolder navMenuItemHolder = null;

    if (convertView == null) {
         LayoutInflater inflater = (LayoutInflater) this._context
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate( R.layout.drawer_list_item, parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById( R.id.navmenuitem_label );
        ImageView iconView = (ImageView) convertView
                .findViewById( R.id.navmenuitem_icon );

        navMenuItemHolder = new NavMenuItemHolder();
        navMenuItemHolder.labelView = labelView ;
        navMenuItemHolder.iconView = iconView ;

        convertView.setTag(navMenuItemHolder);
    }

    if ( navMenuItemHolder == null ) {
        navMenuItemHolder = (NavMenuItemHolder) convertView.getTag();
    }

    navMenuItemHolder.labelView.setText(menuItem.getLabel());
    navMenuItemHolder.iconView.setImageResource(menuItem.getIcon());

    return convertView ;
}

public View getHeaderView(View convertView, ViewGroup parentView,
        NavDrawerItem navDrawerItem) {

    NavMenuHeader menuHeader = (NavMenuHeader) navDrawerItem ;
    NavMenuHeaderHolder navMenuHeaderHolder = null;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate( R.layout.navdrawer_header, parentView, false);
        ImageView labelView = (ImageView) convertView
                .findViewById( R.id.img_header);

        navMenuHeaderHolder = new NavMenuHeaderHolder();
        navMenuHeaderHolder.imgView = labelView ;
        convertView.setTag(navMenuHeaderHolder);
    } else {
        navMenuHeaderHolder = (NavMenuHeaderHolder) convertView.getTag();

    }

    int nIdDrawable = _context.getResources().getIdentifier(menuHeader.getLabel(), "drawable", _context.getPackageName());

    navMenuHeaderHolder.imgView.setImageResource(nIdDrawable);

    return convertView ;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition,
        int childPosition) {
    return true;
}

}

以下是ExpandableListView定义的活动部分:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    setTitle(getResources().getString(R.string.title_activity_primera));

    navConf = getNavDrawerConfiguration();

    navDrawer = (ScrimInsetsFrameLayout)findViewById(R.id.scrimInsetsFrameLayout);


    drawer = (DrawerLayout) findViewById(R.id.drawer);
    expListView = (ExpandableListView) findViewById(R.id.expListViewDrawer); 

    // set a custom shadow that overlays the main content when the drawer opens
    drawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener
    expListView.setAdapter(navConf.getBaseAdapter());
    expListView.setGroupIndicator(null); 

 // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {

            //Si no es itinerarios
            if (groupPosition != 9 && groupPosition != 0)
                selectItem(groupPosition, 0);

            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            //Si no es itinerarios
            int len = navConf.getBaseAdapter().getGroupCount();

            for (int i = 0; i < len; i++) {
                if (i != groupPosition) {
                    expListView.collapseGroup(i);
                }
            }
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {


        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            selectItem(groupPosition, childPosition);
            return false;
        }
    });
.....
}

我一直在调试Android 2.3的应用程序并且应用程序启动正常但是当我单击项目列表时,在方法getGroupView中收到的convertview对于第一个项目列表是错误的。收到的convertview是NavMenuItem的视图,不是NavMenuHeader的视图,然后是应用程序崩溃。其他具有较新Android版本的设备不会发生此问题。这可能是什么问题?

错误如下:

04-16 15:07:58.469: E/AndroidRuntime(3532): FATAL EXCEPTION: main
04-16 15:07:58.469: E/AndroidRuntime(3532): java.lang.ClassCastException: com.flipaos.hermandadcarmendelashuertas.NavDrawerAdapter$NavMenuItemHolder
04-16 15:07:58.469: E/AndroidRuntime(3532):     at com.flipaos.hermandadcarmendelashuertas.NavDrawerAdapter.getHeaderView(NavDrawerAdapter.java:192)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at com.flipaos.hermandadcarmendelashuertas.NavDrawerAdapter.getGroupView(NavDrawerAdapter.java:120)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.AbsListView.obtainView(AbsListView.java:1414)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.ListView.fillSpecific(ListView.java:1290)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.ListView.layoutChildren(ListView.java:1576)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.AbsListView.onLayout(AbsListView.java:1265)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.view.View.layout(View.java:7200)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.view.View.layout(View.java:7200)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:911)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.view.View.layout(View.java:7200)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.view.View.layout(View.java:7200)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
04-16 15:07:58.469: E/AndroidRuntime(3532):     at android.view.View.layout(View.java:7200)

提前致谢。

0 个答案:

没有答案