Amir
Amir

Reputation: 1636

Android Unresolved reference: findNavController error

I am using Kotlin and have all references added in my project.

// Navigation
implementation "android.arch.navigation:navigation-common-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-runtime-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$rootProject.nav_version"

I also have these on top of the build.gradle

apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' apply plugin: 'androidx.navigation.safeargs'

And I am using it like this inside my fragment

class HomeFragment : BaseFragment(){

    ...

    override fun onCategoryItemClicked(category: Category) {
        view.findNavController()?.navigate(R.id.phrasesFragment)
    }
}

I can see this generated extension(file) too

fun Fragment.findNavController(): NavController =
    NavHostFragment.findNavController(this)

Upvotes: 24

Views: 21612

Answers (3)

Rohit Gupta
Rohit Gupta

Reputation: 1

Just add this in your build.gradle(:app) -

plugins {
    id 'kotlin-android-extensions'
}

Upvotes: -1

Ananth
Ananth

Reputation: 2735

In my case, downgrading navigation libraries helped.

I downgraded from 2.3.5 to 2.3.4.

implementation 'androidx.navigation:navigation-ui-ktx:2.3.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4'

2.3.5 was latest stable at the time of writing this answer.

Just try changing version of the library, and see which works. Find versions here.

Upvotes: 1

Amir
Amir

Reputation: 1636

After lots of try and error, I found the source of issue. upgrading my gradle to gradle:3.3.0-alpha06 was the key. I revert it to the previous version and it works fine now. So I think something is happening there which needs to be fixed by #Google.

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha05'

By the way while using tha latest version of the gradle(At the time of writing this, I mean gradle:3.3.0-alpha06), this will work

    Navigation.findNavController(view!!).navigate(R.id.phrasesFragment)

instead of

override fun onCategoryItemClicked(category: Category) {
    view.findNavController()?.navigate(R.id.phrasesFragment)
}

Upvotes: 8

Related Questions