BottomNavigationView = NULL - PullRequest
       21

BottomNavigationView = NULL

0 голосов
/ 09 марта 2020

Android: я пытаюсь проверить элемент bottomNavigationMenu (как если бы я нажал на него), но Android Studio не найдет мое bottomNavigationMenu.

1st try
//        BottomNavigationView bottomNavigationView = (BottomNavigationView) root.findViewById(R.id.nav_view);
//        bottomNavigationView.setSelectedItemId(R.id.navigation_home);
2nd try
//        BottomNavigationView bottomNavigationView;
//        bottomNavigationView = (BottomNavigationView) root.findViewById(R.id.nav_view);
//        bottomNavigationView.setOnNavigationItemSelectedListener(myNavigationItemListener);
//        bottomNavigationView.setSelectedItemId(R.id.navigation_home);

3rd try
//        BottomNavigationView bottomNavigationView = root.findViewById(R.id.nav_view);
//        bottomNavigationView.getMenu().getItem(1).setChecked(true);

4th try
//        BottomNavigationView bottomNavigationView = root.findViewById(R.id.nav_view);
//        Menu menu = bottomNavigationView.getMenu();
//        MenuItem menuItem = menu.getItem(1);
//        menuItem.setChecked(true);
5th try
        Main2Activity a = new Main2Activity();
        BottomNavigationView bottomNavBar = a.findViewById(R.id.nav_view);
        bottomNavBar.getMenu().findItem(R.id.navigation_home).setChecked(true);

Независимо от того, как я это попробую, bottomNavigationView.findViewById (R.id.nav_view) вернет NULL, даже если идентификатор правильный.

Это будет мой XML:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        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:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.Constrai
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...