簡體   English   中英

單擊底部菜單時如何打開側面導航抽屜?

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

這就是我想要實現的

我想要設置菜單單擊將出現在側面導航抽屜中

在此處輸入圖片說明

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錯誤太多代碼無法發布

您沒有正確綁定對象,並導致了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;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM