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.
When using data binding you use setContentView differently:
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());