使用OnClick菜单显示/隐藏子菜单

时间:2016-10-17 19:53:33

标签: javascript android menu navigation-drawer

我对此很新,所以我不知道如何正确解释它,但我想制作菜单,当我点击它时,打开子菜单。那个菜单需要在导航抽屉里(我有)。所以,如果你知道Steam移动应用程序的外观,我想让它像它一样。我已尝试在子菜单上设置可见性,但每当我点击菜单时,应用程序就会崩溃。谢谢你能给我的任何帮助。 这是最重要的代码(至少我的想法):

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem settingsItem = menu.findItem(R.id.action_settings);
    settingsItem.setVisible(false);
    register = menu.findItem(R.id.nav_submenu);
    return false;
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.nav_menu) {
       register.setVisible(!(register.isVisible()));
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

这是log:

  

10-17 22:03:39.467 6307-6307 / com.example.user.app D / AndroidRuntime:关闭VM   10-17 22:03:39.467 6307-6307 / com.example.user.app E / AndroidRuntime:FATAL EXCEPTION:main                                                                              处理:com.example.user.app,PID:6307                                                                              java.lang.NullPointerException:尝试在空对象引用上调用接口方法'boolean android.view.MenuItem.isVisible()'                                                                                  at com.example.user.app.MainActivity.onNavigationItemSelected(MainActivity.java:132)                                                                                  在android.support.design.widget.NavigationView $ 1.onMenuItemSelected(NavigationView.java:153)                                                                                  在android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)                                                                                  在android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)                                                                                  在android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)                                                                                  在android.support.design.internal.NavigationMenuPresenter $ 1.onClick(NavigationMenuPresenter.java:328)                                                                                  在android.view.View.performClick(View.java:5233)                                                                                  在android.view.View $ PerformClick.run(View.java:21209)                                                                                  在android.os.Handler.handleCallback(Handler.java:739)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:152)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5507)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)   10-17 22:03:39.467 6307-6307 / com.example.user.app D / AppTracker:App事件:崩溃   10-17 22:03:39.523 6307-6307 / com.example.user.app I / Process:发送信号。 PID:6307 SIG:9

1 个答案:

答案 0 :(得分:0)

我相信你要找的是ExpandableListView