android应用程序中的ClassCastException

时间:2015-01-08 12:15:13

标签: java android eclipse android-layout listview

我有点问题。我想用标题创建导航抽屉,但是eclipse会抛出一个类强制转换异常。问题在于适配器,但我找不到它。你能帮我么。 这是我的适配器的代码

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;
}

}

这是我的主要活动的代码

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

  

致命的例外:主要    java.lang.ClassCastException:com.markoKurt.zeljko.NavDrawerAdapter $ NavMenuSectionHolder无法强制转换为com.markoKurt.zeljko.NavDrawerAdapter $ NavMenuItemHolder       在com.markoKurt.zeljko.NavDrawerAdapter.getItemView(NavDrawerAdapter.java:52)       在com.markoKurt.zeljko.NavDrawerAdapter.getView(NavDrawerAdapter.java:25)       在android.widget.AbsListView.obtainView(AbsListView.java:2449)       在android.widget.ListView.measureHeightOfChildren(ListView.java:1250)       在android.widget.ListView.onMeasure(ListView.java:1162)       在android.view.View.measure(View.java:15504)       在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:617)       在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)       在android.view.View.measure(View.java:15504)       在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:866)       在android.view.View.measure(View.java:15504)       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)       在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)       在android.view.View.measure(View.java:15504)       在android.widget.LinearLayout.measureVertical(LinearLayout.java:833)       在android.widget.LinearLayout.onMeasure(LinearLayout.java:574)       在android.view.View.measure(View.java:15504)       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)       在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)       在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2385)       在android.view.View.measure(View.java:15504)       在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)       在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)       在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)       在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)       在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4526)       在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725)       在android.view.Choreographer.doCallbacks(Choreographer.java:555)       在android.view.Choreographer.doFrame(Choreographer.java:525)       在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711)       在android.os.Handler.handleCallback(Handler.java:615)       在android.os.Handler.dispatchMessage(Handler.java:92)       在android.os.Looper.loop(Looper.java:137)       在android.app.ActivityThread.main(ActivityThread.java:4921)       at java.lang.reflect.Method.invokeNative(Native Method)       在java.lang.reflect.Method.invoke(Method.java:511)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1027)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)       在dalvik.system.NativeStart.main(本地方法)

请帮帮我,并提前致谢

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

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

@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();
}

答案 2 :(得分:1)

您尝试将navDrawerItem强制转换为navMenuItem

NavMenuItem menuItem = (NavMenuItem) navDrawerItem;

更改它
NavDrawerItem drawerItem = (NavDrawerItem ) navDrawerItem;

并做相应的事情。

答案 3 :(得分:1)

答案 4 :(得分:0)

在getItemView和getSectionView中,您可以获得不同的视图,因此您需要检查类实例。

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()
}