我想用另一个菜单替换一个菜单,在这个视图中我有物品回家,产品等..,同时点击产品它打开另一个包含差异产品的菜单。在下面的代码中我试图替换另一个菜单,但我得到空指针异常,但我想知道在点击导航项目我如何切换到另一个导航视图? 任何人都可以建议在这种情况下做什么?I have attached logcat snapshot.
public void selectDrawerItem(MenuItem menuItem)
{
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId())
{
case R.id.menu_home:
fragmentClass = A.class;
break;
case R.id.menu_products:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.menu_products);
break;
case R.id.menu_B:
fragmentClass = B.class;
break;
case R.id.menu_C:
fragmentClass = C.class;
break;
case R.id.menu_D:
fragmentClass = d.class;
break;
case R.id.menu_E:
fragmentClass = E.class;
break;
case R.id.menu_F:
fragmentClass = F.class;
break;
default:
fragmentClass = A.class;
}
try
{
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
//this is line 155:
fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
drawer.closeDrawers();
}
private void setupDrawerContent(NavigationView navigationView){
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(MenuItem menuItem)
{
selectDrawerItem(menuItem);
return true;
}
});
}