android 应用程序中的 ClassCastException

ClassCastException in android application

我有个小问题。我想创建带标题的导航抽屉,但 Eclipse 向我抛出一个 class 转换异常。问题出在适配器中,但我找不到它。你能帮我么。 这是我的适配器的代码

public class NavDrawerAdapter extends ArrayAdapter<NavDrawerItem>{

private LayoutInflater inflater;

public NavDrawerAdapter(Context context, int textViewResourceId, NavDrawerItem[] objects ) {
    super(context, textViewResourceId, objects);
    this.inflater = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null ;
    NavDrawerItem menuItem = this.getItem(position);
    if ( menuItem.getType() == NavMenuItem.ITEM_TYPE ) {
        view = getItemView(convertView, parent, menuItem );
    }
    else {
        view = getSectionView(convertView, parent, menuItem);
    }
    return view ;
}

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

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

    if (convertView == null) {
        convertView = inflater.inflate( R.layout.navdrawer_item, parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById( R.id.navmenuitem_label );
        ImageView i = (ImageView) convertView.findViewById(R.id.navmenuitem_icon);

        navMenuItemHolder = new NavMenuItemHolder();
        navMenuItemHolder.labelView = labelView ;
        navMenuItemHolder.image = i;

        convertView.setTag(navMenuItemHolder);
    }

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

    navMenuItemHolder.labelView.setText(menuItem.getLabel().toString());

    return convertView ;
}

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

    NavMenuSection menuSection = (NavMenuSection) navDrawerItem ;
    NavMenuSectionHolder navMenuItemHolder = null;

    if (convertView == null) {
        convertView = inflater.inflate( R.layout.navdrawer_section, parentView, false);
        TextView labelView = (TextView) convertView
                .findViewById( R.id.navmenusection_label );

        navMenuItemHolder = new NavMenuSectionHolder();
        navMenuItemHolder.labelView = labelView ;
        convertView.setTag(navMenuItemHolder);
    }

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

    navMenuItemHolder.labelView.setText(menuSection.getLabel().toString());

    return convertView ;
}

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

private static class NavMenuSectionHolder {
    private TextView labelView;
}

}

这是我的主要代码 activity

public class MainActivity extends Activity{

private DrawerLayout drawerLayout;
private NavDrawerAdapter adapter;
private ListView mDrawerList;
private ImageView menu;
private RelativeLayout rightRL;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    menu = (ImageView) findViewById(R.id.imageView1);
    rightRL = (RelativeLayout) findViewById(R.id.whatYouWantInRightDrawerProfil);

    NavDrawerItem[] m = new NavDrawerItem[]{
            NavMenuSection.create(100, "Demos"),
            NavMenuItem.create(101, "kako si", false, this),
            NavMenuSection.create(200, "General"),
            NavMenuItem.create(202, "Rate this app", false, this)
    };
    adapter = new NavDrawerAdapter(this, R.layout.navdrawer_item, m);
    mDrawerList.setAdapter(adapter);


}

public void onRight(View view) {
    drawerLayout.openDrawer(rightRL);
}

}

这是我的 logcat 来自 eclipse

FATAL EXCEPTION: main java.lang.ClassCastException: com.markoKurt.zeljko.NavDrawerAdapter$NavMenuSectionHolder cannot be cast to com.markoKurt.zeljko.NavDrawerAdapter$NavMenuItemHolder at com.markoKurt.zeljko.NavDrawerAdapter.getItemView(NavDrawerAdapter.java:52) at com.markoKurt.zeljko.NavDrawerAdapter.getView(NavDrawerAdapter.java:25) at android.widget.AbsListView.obtainView(AbsListView.java:2449) at android.widget.ListView.measureHeightOfChildren(ListView.java:1250) at android.widget.ListView.onMeasure(ListView.java:1162) at android.view.View.measure(View.java:15504) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) at android.view.View.measure(View.java:15504) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:866) at android.view.View.measure(View.java:15504) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:15504) at android.widget.LinearLayout.measureVertical(LinearLayout.java:833) at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) at android.view.View.measure(View.java:15504) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385) at android.view.View.measure(View.java:15504) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) at android.view.Choreographer.doCallbacks(Choreographer.java:555) at android.view.Choreographer.doFrame(Choreographer.java:525) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4921) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) at dalvik.system.NativeStart.main(Native Method)

请帮助我,在此先感谢

您试图将 navDrawerItem 转换为 navMenuItem

NavMenuItem menuItem = (NavMenuItem) navDrawerItem;

改成

NavDrawerItem drawerItem = (NavDrawerItem ) navDrawerItem;

并相应地做..

您的问题与您只得到一个空 convertView 这一事实有关,因此标签内容只为 NavMenuSectionHolderNavMenuItemHolder 设置一次。通常,当您必须处理多个 convertView 时,您可以覆盖 getViewTypeCountgetItemViewType。在您的情况下,两个持有人 class 仅 ImageView 不同。所以我建议你只有一个支架 class,(膨胀一种布局),并在不需要时隐藏 ImageView

尝试像这样改变不同布局的逻辑, http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html

我解决了这个问题。我只需要添加

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

@Override
public int getItemViewType(int position) {
    return this.getItem(position).getType();
}

@Override
public boolean isEnabled(int position) {
    return getItem(position).isEnabled();
}

在getItemView和getSectionView中可以得到不同的视图,所以需要检查class实例。

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

NavMenuSection menuSection;
if (navDrawerItem !=null)
if (navDrawerItem instanceof NavMenuSection){
menuSection = navDrawerItem}
else {
menuSection = new NavMenuSection()
}else {
menuSection = new NavMenuSection()
}
public View getItemView( View convertView, ViewGroup parentView, NavDrawerItem navDrawerItem ) {

NavMenuItem menuItem = (NavMenuItem) navDrawerItem ;
    NavMenuItemHolder navMenuItemHolder = null;
NavMenuSection menuSection;
if (menuItem!=null)
if (menuItem instanceof  NavMenuItem){
menuItem =  NavMenuItem
else {
menuItem = new  NavMenuItem()
}else {
menuItem= new  NavMenuItem()
}