Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 439
Amir Avatar asked Aug 17 '18 07:08

Amir


Video Answer


2 Answers

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)
}
like image 53
Amir Avatar answered Oct 16 '22 14:10

Amir


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.

like image 1
Ananth Avatar answered Oct 16 '22 15:10

Ananth