Issue
I'm trying to create a BottomNavigationBar in my Android app, and I keep getting this error:
Cannot resolve method 'setupWithNavController(com.google.android.material.bottomnavigation.BottomNavigationItemView, androidx.navigation.NavController)'
Not sure what I'm doing wrong since these 3 lines of code in the activity are found in many nav bar guides online.
Activity Code Segment in Question
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
BottomNavigationItemView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController); //cannot resolve this
}
Relevant Dependencies
implementation "androidx.navigation:navigation-fragment:2.3.1"
implementation "androidx.navigation:navigation-ui:2.3.1"
activity_navigation.xml
<?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=".NavigationActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settingsNavFragment"
android:icon="@drawable/ic_baseline_settings_24"
android:title="Settings" />
<item
android:id="@+id/homeNavFragment"
android:icon="@drawable/ic_baseline_home_24"
android:title="Home" />
<item
android:id="@+id/groupsNavFragment"
android:icon="@drawable/ic_baseline_groups_24"
android:title="Groups" />
</menu>
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/homeNavFragment">
<fragment
android:id="@+id/homeNavFragment"
android:name="com.example.foodwithfriends.HomeNavFragment"
android:label="fragment_home_nav"
tools:layout="@layout/fragment_home_nav" />
<fragment
android:id="@+id/settingsNavFragment"
android:name="com.example.foodwithfriends.settingsNavFragment"
android:label="fragment_settings_nav"
tools:layout="@layout/fragment_settings_nav" />
<fragment
android:id="@+id/groupsNavFragment"
android:name="com.example.foodwithfriends.GroupsNavFragment"
android:label="fragment_groups_nav"
tools:layout="@layout/fragment_groups_nav" />
</navigation>
Solution
You've casted your BottomNavigationView
incorrectly.
Replace
BottomNavigationItemView bottomNavigationView = findViewById(R.id.bottomNavigationView);
With
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
Answered By - ianhanniballake
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.