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

时间:2019-07-15 03:07:13

标签: java android navigation-drawer bottomnavigationview

这就是我要实现的目标

我想要设置菜单单击将出现在侧面导航抽屉中

enter image description here

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

这是我得到的错误

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

编辑:我试图在此处粘贴一些代码,但是StackOverflow错误太多代码无法发布

2 个答案:

答案 0 :(得分:0)

您没有正确绑定对象,并导致了NullPointerException。

请粘贴您的代码,我们会为您提供帮助。

我看到了一个奇怪的部分,但也许此错误仍会出现,您可能需要粘贴您的layout.xml

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


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

应修改为

mMainNav.setItemIconTintList(null);

您可以使用

检查它是否为空。
getActivity().findViewById(R.id.XXX)

更新

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

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

答案 1 :(得分:0)

花了我4天的时间解决这个问题。原来,您需要在活动布局中设置抽屉式布局。我的问题是来自不同xml的抽屉布局,这就是为什么返回null的原因。感谢那些试图帮助我的人

<?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>