【问题标题】:How to open the side navigation drawer when click on bottom menu?单击底部菜单时如何打开侧面导航抽屉?
【发布时间】:2019-07-15 03:07:13
【问题描述】:

这就是我想要实现的目标

我想要设置菜单点击会出现侧导航抽屉

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 错误太多代码无法发布

【问题讨论】:

    标签: java android navigation-drawer bottomnavigationview


    【解决方案1】:

    您没有正确绑定对象并导致 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;
    

    【讨论】:

    • 是的,我因为太多代码被 SO 阻止了。现在我设法编辑了一些。请检查一下谢谢!
    • 我不认为这是问题所在。 mMainNav 用于 btmnav 控制其上方的片段。我想要的是单击 btmnav 菜单之一以显示侧 navdrawer,即drawerlayout.openDrawer(GravityCompat.END) 返回空错误。
    • 无法解析方法 'getView()' 先生。我也尝试使用 getActivity() 作为其他已回答的问题,但仍然无法解决方法
    【解决方案2】:

    我花了 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      相关资源
      最近更新 更多