NavigationRailView is causing None of the following candidates is applicable because of receiver type mismatch

Viewed 165

Material Components are great, I have to work with Navigation rail component with Navigation Controllers, but could not figure out how to set up this, I have layout file as:

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/inner_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:foregroundTint="?attr/colorPrimaryVariant"
            android:theme="@style/Theme.MEM.AppBarOverlay"
            app:liftOnScroll="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/Theme.MEM.PopupOverlay"
                app:titleTextColor="@color/white" />

        </com.google.android.material.appbar.AppBarLayout>

        <include
            android:id="@+id/content_main"
            layout="@layout/content_main"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/navigation_rail"
            app:layout_constraintTop_toBottomOf="@id/app_bar" />

        <com.google.android.material.navigationrail.NavigationRailView
            style="@style/Widget.MaterialComponents.NavigationRailView.Colored.Compact"
            android:id="@+id/navigation_rail"
            android:layout_width="80dp"
            android:layout_height="0dp"
            app:elevation="0dp"
            app:headerLayout="@layout/layout_header_rail"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/app_bar"
            app:menu="@menu/rail_navigation" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

and MainActivity.kt is simple as:

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding
    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.appBarMain.toolbar)

        binding.appBarMain.fab?.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }

        // In favor of androidx.fragment.app.FragmentContainerView in XML file
        val navHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host_fragment_content_main) as NavHostFragment

        navController = navHostFragment.navController

        binding.appBarMain.navigationRail?.let {
            appBarConfiguration = AppBarConfiguration(
                setOf(
                    R.id.nav_current, R.id.nav_history, R.id.nav_settings
                ),
                binding.drawerLayout
            )
            setupActionBarWithNavController(navController, appBarConfiguration)
            it.setupWithNavController(navController) // it causes issue of None of the following candidates is applicable because of receiver type mismatch:
        }

        // Omitting rest of the stuff

    }

}

on lines above:

it.setupWithNavController(navController) // it causes issue of None of the following candidates is applicable because of receiver type mismatch:

it causes this:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Toolbar.setupWithNavController(navController: NavController, drawerLayout: DrawerLayout?): Unit defined in androidx.navigation.ui
public fun Toolbar.setupWithNavController(navController: NavController, configuration: AppBarConfiguration = ...): Unit defined in androidx.navigation.ui
public fun CollapsingToolbarLayout.setupWithNavController(toolbar: Toolbar, navController: NavController, drawerLayout: DrawerLayout?): Unit defined in androidx.navigation.ui
public fun CollapsingToolbarLayout.setupWithNavController(toolbar: Toolbar, navController: NavController, configuration: AppBarConfiguration = ...): Unit defined in androidx.navigation.ui
public fun BottomNavigationView.setupWithNavController(navController: NavController): Unit defined in androidx.navigation.ui
public fun NavigationView.setupWithNavController(navController: NavController): Unit defined in androidx.navigation.ui

But When I look for around I found it is used in Google I/O Android App at this line.

How can I fix this issue please any suggestions.

0 Answers
Related