简体   繁体   English

单击底部菜单时如何打开侧面导航抽屉?

[英]How to open the side navigation drawer when click on bottom menu?

This is what i'm trying to achieve 这就是我想要实现的

I want settings menu click will appear side nav drawer 我想要设置菜单单击将出现在侧面导航抽屉中

在此处输入图片说明

HomeActivity.java HomeActivity.java

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

    mMainFrame  =   (FrameLayout) findViewById(R.id.main_frame);
    mMainNav    =   (BottomNavigationView) findViewById(R.id.main_nav);

    homeFragment         =   new HomeFragment();
    analyticsFragment    =   new AnalyticsFragment();
    paymentFragment      =   new PaymentFragment();
    settingsFragment     =   new SettingsFragment();

    drawerLayout = findViewById(R.id.drawerlayout);

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.main_frame,new HomeFragment()).commit();

    mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            switch (menuItem.getItemId()) {
                case R.id.navigation_home:
                    setFragment(homeFragment);
                    return true;

                case R.id.navigation_analytics:
                    setFragment(analyticsFragment);
                    return true;

                case R.id.navigation_payment:
                    setFragment(paymentFragment);
                    return true;

                case R.id.navigation_settings:
                    drawerLayout.openDrawer(GravityCompat.END);
                    return true;

                default:
                    return false;
            }
        }
    });

    BottomNavigationView navView = findViewById(R.id.main_nav);
    navView.setItemIconTintList(null);

And this is the error i get 这是我得到的错误

Unable to start activity ComponentInfo{com.example.ewallet/com.example.ewallet.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference

EDIT : I tried to paste some of my code here but StackOverflow error too many code to post 编辑:我试图在这里粘贴一些代码,但是StackOverflow错误太多代码无法发布

You didn't bind your object correctly and caused a NullPointerException. 您没有正确绑定对象,并导致了NullPointerException。

Please paste your code and we can help you. 请粘贴您的代码,我们可以为您提供帮助。

I saw a weird part, but maybe this error will still appear, you may need to paste your layout.xml 我看到了一个奇怪的部分,但也许此错误仍会出现,您可能需要粘贴您的layout.xml

mMainNav    =   (BottomNavigationView) findViewById(R.id.main_nav);


BottomNavigationView navView = findViewById(R.id.main_nav);
navView.setItemIconTintList(null);

Should be modify to 应该修改为

mMainNav.setItemIconTintList(null);

You can check if it is null with 您可以使用以下命令检查它是否为空

getActivity().findViewById(R.id.XXX)

UPDATE 更新

    drawerLayout = (DrawerLayout)getView().findViewById(R.id.drawerlayout);

    case R.id.navigation_settings:
                drawerLayout.openDrawer(drawerLayout);
                return true;

Took me 4 days to figure this out. 花了我4天时间解决这个问题。 Turned out you need to have drawerlayout in your activity layout. 原来,您需要在活动布局中设置抽屉式布局。 My problem was drawerlayout from different xml that's why returned null. 我的问题是来自不同xml的抽屉布局,这就是为什么返回null的原因。 Thx for those who tried for helping me 感谢那些试图帮助我的人

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="end">

    <include
        layout="@layout/app_bar_settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_settings"
        app:menu="@menu/activity_settings_drawer" />

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    tools:context=".HomeActivity">

    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/main_nav"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:labelVisibilityMode="labeled"
        app:itemTextColor="@color/colorPrimaryDark"
        app:menu="@menu/bottom_nav_menu" />
    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.DrawerLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在底部导航菜单上恢复片段单击? - How to resume fragment on Bottom navigation menu click? 打开幻灯片菜单时隐藏底部导航视图 - hiding bottom navigation view when open slide menu 如何使用导航抽屉的底部导航 - How to use Bottom Navigation with Navigation Drawer 通过单击导航抽屉项目打开 URL - Open URL by click on navigation drawer item 打开导航抽屉,其中单击事件具有布局 - Open Navigation Drawer with layout on click event 如何一起使用底部导航栏和侧边导航栏,我的侧边导航按钮不显示时我但底部导航 - How to use bottom navigation bar and side navigation bar together, my side navigation button is not showing when i but bottom navigation 单击导航抽屉菜单选项时,什么都没有显示? - Nothing shows up when i click my Navigation drawer menu options? 当我单击回收站视图中的项目时,我可以使用导航抽屉作为下拉菜单吗? - Can i use a navigation drawer as a dropdown menu when i click on an item in a recyclerview? 如何在appium中打开和访问导航侧抽屉? 我正在使用Java语言在Appium中实现自动化 - How to open & access navigation side drawer in appium ? I am using java language for automation in appium 打开片段时导航抽屉应用程序行为 - Navigation Drawer app behaivour when open fragments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM