https://kotlinlang.org logo
Title
d

Daniele Segato

08/20/2018, 8:10 AM
I've upgraded to recently released navigation component alpha05 and this error popped up:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun `Activity;`.findNavController(viewId: Int): `NavController;` defined in androidx.navigation
public fun `View;`.findNavController(): `NavController;` defined in androidx.navigation
This means my previously working code doesn't compile anymore on this stuff:
view.setOnClickListener {
    it.findNavController().navigate(somewhere)
}
cause it cant resolve
findNavController()
extension function. I think it's a collision? Checked the source code
package androidx.navigation

import android.view.View
/**
 * Find a [NavController] associated with a [View].
 *
 * Calling this on a View not within a [NavHost] will result in an
 * [IllegalStateException]
 */
fun View.findNavController(): NavController =
        Navigation.findNavController(this)
and
package androidx.navigation
import android.app.Activity
import android.support.annotation.IdRes
/**
 * Find a [NavController] given the id of a View and its containing
 * [Activity].
 *
 * Calling this on a View that is not a [NavHost] or within a [NavHost]
 * will result in an [IllegalStateException]
 */
fun Activity.findNavController(@IdRes viewId: Int): NavController =
        Navigation.findNavController(this, viewId)
these two collide I think and when you
import androidx.navigation.findNavController
it doesn't know which one to import?
I know I can just replace it with
Navigation.findNavController(it)
. I've asked the question to learn more about this kind of situation. How to handle collisions like this, how to avoid them etc...
l

louiscad

08/20/2018, 8:16 AM
Isn't
it
in
setOnClickListener { ... }
nullable? Try with safe call (
it?.findNavController()
)
d

Daniele Segato

08/20/2018, 8:29 AM
Same thing happens, it is of type
View!
anyway, so yeah, can be Nullable but basically never is. And if i use
it!!
or
it?
nothing change
(I've created a bug report) https://issuetracker.google.com/issues/112806420 in the meanwhile I hope to learn more on how this works