0

I try to implement a BottomNavigationMenu in my app using the Jetpack navigation. I followed each step of this tutorial https://www.youtube.com/watch?v=pT_4rV3gO78 but unfortunately I could not integrate into my app and I get an error when starting the app

java.lang.RuntimeException: Unable to start activity ...

     Caused by: java.lang.NullPointerException: Attempt to read from field 'com.google.android.material.bottomnavigation.BottomNavigationView com.example.td.barapp.databinding.ActivityMainBinding.bottomNavigation' on a null object reference
        at com.example.td.barapp.MainActivity.onCreate(MainActivity.java:28)

The error pinpoints to my main activy which has the follwing java code:

package com.example.td.barapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;

import com.example.td.barapp.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity  {

   
    private ActivityMainBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        DataBaseEntries.createDataBaseEntries(drinksDB);

        NavController navController = Navigation.findNavController(this, R.id.navHostfragment);

        NavigationUI.setupWithNavController(binding.bottomNavigation,navController );


    }
}

So it says that the NavController is null. Why is it null and how can make it not null?

Here you can see the XML file of the Main Activity with the NavHostFragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity"
    tools:ignore="ExtraText">


    <fragment
        android:id="@+id/navHostfragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        app:labelVisibilityMode="labeled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorGreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation"
        app:itemIconTint="@color/colorPrimaryDark"
        app:itemTextColor="@color/colorAccent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

And here you can see the XML file of the ButtonNavigationMenu, in which I linked the destination to the ID of the NavGraph (android:id="@+id/FR_LanguageSelection"):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item
        android:id="@+id/FR_LanguageSelection"
        android:icon = "@drawable/ic_add_circle_full"
        android:title = "Language" />



</menu>

Do you have an idea why I can't start the app and get this error? Without the BottomNavigationMenu in the JavaCode the app works and the BottomNavigationView is displayed correctly (but of course I can't use it for navigation). I'd appreciate every answer and would be quite thankful for your help.

2
  • can you share your nav_graph.xml as well? Commented Jan 10, 2021 at 11:57
  • Hi ErfanDP. Thanks for your comment. Unfortunately I am not allowed to share the NavGraph. Is there anything I should consider there? Commented Jan 10, 2021 at 12:11

2 Answers 2

1

When using data binding you use setContentView differently:

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Sign up to request clarification or add additional context in comments.

18 Comments

Thanks for your answer Dorian. When using your suggested code I get the following runtime error: com.example.td.barapp:layout/activity_main: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #11 in com.example.td.barapp:layout/activity_main: Error inflating class fragment Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Duplicate id 0x7f0a0110, tag null, or parent id 0xffffffff with another fragment for androidx.navigation.fragment.NavHostFragment
Okay, I put everything in the oncreateView method and now I at least do not get the starting null error and the app can be started. However, the navigation does not work. When I click on the button of the ButtomNavigationBar nothing happens
For that you would need to add more items into your menu.xml and add fragments to navigation.xml. Check if your ID's match. And I also noticed I missed one line in my answer, I have just edited it
Thanks for your answer Dorian. I upvoted it. Can you tell me what I have to specify in the navigation graph in order to make the navigation work? Bascially at the moment the fragments can be seen in the navigation graph but they are not linked
There are dozens of tutorials for that implementation on the internet, this is official doc: developer.android.com/guide/navigation/…
|
1

there are a few things off in my eyes

-I don't think it's the issue but for data binding, you need to wrap your XML file in <layout> attribute I guess you just didn't put it here but if that's the case fix it.

-you need to be sure that your menu item id witch is FR_LanguageSelection here be the same as your destination fragment id

-you need to navigate using actions that are declared in your nav_graph but there is nothing here

check these and if these are not the case let me know

14 Comments

Thanks ErfanDP for your answer and effort. Basically I do not understand your first point with the <layout>. As said in the question, without the Java Code of the Bottom Navigation everything is displayed correctly (including the BottomNavigationMenu) but I just can't navigate. Regarding your second point, I just copied the id from the navGraph so it is the same
Regarding your 3rd point with the navGraph. Bascially my NavGraph has more than 20 entries (as I use more than 20 different fragments). The navigation without the BottomNavigationMenu works fine using the Jetpack components. Shall I have a link from each of the 20 Fragments to the destination fragment (FR_LanguageSelection) of the BottomNavigationView?
@VanessaF for DataBinding classes to be generated you need to wrap your entire XML fragments and activities that want to use data binding with layout attribute here is my fairly incomplete project but I completely implement DataBinding and NavCombponent with bottom nav with this you probably will understand my points : github.com/ErfanDP/WoocommerceMarket check out list XML files for example for my first point and look at navigating in my single Activity
Okay, I put everything in the oncreateView method and now I at least do not get the starting null error and the app can be started. However, the navigation does not work. When I click on the button of the ButtomNavigationBar nothing happens
This is my onCreateView method now "public View onCreateView({at}NonNull LayoutInflater inflater, {at}Nullable ViewGroup container, {at}Nullable Bundle savedInstanceState) { binding = ActivityMainBinding.inflate(inflater, container, false); NavController navController = Navigation.findNavController(this, R.id.navHostfragment); NavigationUI.setupWithNavController(binding.bottomNavigation,navController ); return binding.getRoot(); }"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.