0

Hello I am having a lot of problems with the implementation of this. i have the following xml at main activity

content_main...

    <LinearLayout 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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MainActivity"
        android:orientation="vertical"
        tools:showIn="@layout/app_bar_main">

      <fragment
            android:id="@+id/nav_host"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/bottom_nav_usuario_auto"
            app:defaultNavHost="true"
            android:layout_weight="6">

        </fragment>

        <FrameLayout
            android:id="@+id/bottom_nav_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.8"
           />

    </LinearLayout>

As you can see I am using a frame layout to change the Bottom Navigation view depending on certain conditions in my app I have to use like 4 BNViews. So then i put a bottom Nav view in the frame layout to start the exercise.

     BottomNavUsuarioAuto bottomNavUsuario=new BottomNavUsuarioAuto();
     FragmentManager fragmentManager = getSupportFragmentManager();
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
     fragmentTransaction.add(R.id.bottom_nav_container, bottomNavUsuario);
        fragmentTransaction.commit();

This is is the Bottom config and fragment_bottom_nav_usuario_auto

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".BottomNavUsuarioAuto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    <!-- TODO: Update blank fragment layout -->
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/nav_usuario_auto"
        android:layout_gravity="start"
        android:text="@string/hello_blank_fragment" />
    </LinearLayout>

</FrameLayout>

Then the menu...

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:title="Inicio"
        android:id="@+id/fragmento_a_usuario_auto"
        android:icon="@drawable/ic_menu_camera"
        android:iconTint="#FF9800"
        />
    <item android:title="Mi carro"
        android:icon="@drawable/ic_menu_manage"
        android:id="@+id/fragmento_b_usuario_auto"
        android:iconTint="#FF9800"
        />
    <item android:title="Comercial"
        android:id="@+id/fragmento_c_usuario_auto"
        android:icon="@drawable/ic_menu_send"
        android:iconTint="#FF9800"
        />
    <item
        android:title="Cita taller"
        android:id="@+id/fragmento_d_usuario_auto"
        android:icon="@drawable/ic_menu_share"
        android:iconTint="#FF9800"

        />
</menu>

Then I made shure that the ids in the navigation graphs match the one in the menu..

Then i made sure that i called in the MainActivity

NavController navController= Navigation.findNavController(this,R.id.nav_host);
BottomNavigationView bottomNavigationView=findViewById(R.id.bottom_nav_switcher);           
NavigationUI.setupWithNavController(bottomNavigationView,navController);

I am seeing two types of errors in this process...

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.franco.mac.jetpacknavigation2/com.franco.mac.jetpacknavigation2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference

and:

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.franco.mac.jetpacknavigationexample/com.franco.mac.jetpacknavigationexample.MainActivity}: android.view.InflateException: `Binary XML file line #11: Binary XML file line #12: Error inflating class fragment`

PLEASE DO NOT ANSWER IF YOU DONT REALLY NOW THE ANSWER. I want to know the correct way to complete this exercise, please.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Felipe Franco
  • 171
  • 3
  • 15
  • The code is not complete. Maybe when you call `BottomNavigationView bottomNavigationView=findViewById(R.id.bottom_nav_switcher)`, there isn't the view and the `bottomNavigationView` is `null`. – Gabriele Mariotti Oct 14 '19 at 17:23
  • Hello, Gabriele, i commented out code and there is eventually a view I get it in the frameLayout so this is not the case. – Felipe Franco Oct 14 '19 at 17:41
  • It depends where you are calling this code and if the fragment is just initialized. – Gabriele Mariotti Oct 14 '19 at 17:43

0 Answers0