Android Unresolved reference: findNavController error
Asked Answered
A

3

24

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)
Afb answered 17/8, 2018 at 7:4 Comment(4)
I guess view is not a fragmentCarleecarleen
NavHostFragment.findNavController(fragment) is applicable for a container fragment containing child fragments. If you want to access the NavController for a fragment definition in the activity use View.findNavController() methodTamera
@TimCastelijns it's a fragmentAfb
@Tamera it's inside the fragment.Afb
A
8

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)
}
Afb answered 20/8, 2018 at 5:45 Comment(4)
Thanks for the heads up, this was a very frustrating issue for me as well. New Gradle update borked half of the dependencies i worked with. Made today a wild goose chase! Come on Google! Do BETTER!Fidel
I also noticed this but at first I tough was something else cause I upgraded different things along with the gradle plugin issuetracker.google.com/issues/112806420Groceryman
@DanieleSegato I raised an issue against Gradle but it turned out its google or android studio problem so I don't know where to raise it!Afb
There are multiple issue about this on the Android issue tracker, just go star them :-)Groceryman
C
1

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.

Cassock answered 27/5, 2021 at 16:11 Comment(0)
F
-1

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

plugins {
    id 'kotlin-android-extensions'
}
Faber answered 22/8, 2022 at 13:41 Comment(2)
kotlin-android-extensions is deprecated best not using it anymore developer.android.com/topic/libraries/view-binding/migrationAfb
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Chalk

© 2022 - 2024 — McMap. All rights reserved.