带标题的导航抽屉,indexOutOfBoundsException:length = 9;索引= 9

时间:2015-04-27 08:05:12

标签: android navigation drawer

我有导航抽屉,一切都很好。但我想添加标题。在这种情况下,当我点击抽屉的最后一个元素时,我收到此错误。

java.lang.ArrayIndexOutOfBoundsException: length=9; index=9

这是我的主要代码:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);{

    mTitle = mDrawerTitle = getTitle();

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    //Drawer Layout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    //Drawer list
    mDrawerList = (ListView) findViewById(R.id.main_list_slidermenu);

    //Drawer header image
    View header=getLayoutInflater().inflate(R.layout.drawer_header, null);

    //Establecemos el header
    mDrawerList.addHeaderView(header);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Avisos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Instalaciones
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
    // Buscar por Localización
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
    // Buscar por Actividad
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
    // Buscar por Espacios
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
    // Buscar por Horarios
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
    // Favoritos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
    // Configuración
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(5, -1)));
    //navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));

    // Recycle the typed array
    navMenuIcons.recycle();


    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);


    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };

    //mDrawerToggle.setDrawerIndicatorEnabled(false);
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

和SlideMenuClickListener实现了ListView.OnItemClickListener和lauch displayView方法。

displayView方法代码是:

    /**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        //Do nothing. Because this is image
        break;

    case 1:
        fragment = new HomeFragment();
        break;
    case 2:
        fragment = new RssAvisosFragment();

        break;
    case 3:
        fragment = new TodasLasInstalacionesFragment();
        break;
    case 4:
        fragment = new BuscarInstalacionesDistanciaFragment();
        break;
    case 5:
        fragment = new BuscarInstalacionesActividadFragment();
        break;
    case 6:

        fragment = new PagesFragment();
        break;
    case 7:

        fragment = new BuscarInstalacionesHorarioFragment();
        break;
    case 8:

        fragment = new FavoritosInstalacionesFragment();
        break;
    case 9:

                fragment = new PreferenceFragment();

        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.main_frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

修改 这是NavDrawerListAdapter代码:

public class NavDrawerListAdapter extends BaseAdapter{

private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
    this.context = context;
    this.navDrawerItems = navDrawerItems;
}

@Override
public int getCount() {
    return navDrawerItems.size();
}

@Override
public Object getItem(int position) {       
    return navDrawerItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawerlist_item, null);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.drawerlist_item_icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.drawerlist_item_title);
    TextView txtCount = (TextView) convertView.findViewById(R.id.drawerlist_item_counter);

    imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
    txtTitle.setText(navDrawerItems.get(position).getTitle());

    // displaying count
    // check whether it set visible or not
    if(navDrawerItems.get(position).getCounterVisibility()){
        txtCount.setText(navDrawerItems.get(position).getCount());
    }else{
        // hide the counter view
        txtCount.setVisibility(View.GONE);
    }

    return convertView;
}

}

修改

SlideMenuClickListener代码是:

    /**
 * Slide menu item click listener
 * */
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        displayView(position);
    }
}

1 个答案:

答案 0 :(得分:1)

<强>解决。

错误在此行(displayVIew方法):

setTitle(navMenuTitles[position])

解决方案是:

setTitle(navMenuTitles[position-1])