androidkotlinanko

'receiver type mismatch' with Fragment and Anko toast


I am trying to use Jetbrains' Anko library to easily display an Android toast message in my app.

Here is the relevant code snippet:

val message : CharSequence = "Recycled: ${holder.taskEditText.text}"
(tasksFragment as Fragment).toast(text = message)

and the error:

Error:(80, 45) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline fun Fragment.toast(text: CharSequence): Unit defined in org.jetbrains.anko
public inline fun Fragment.toast(textResource: Int): Unit defined in org.jetbrains.anko
public fun Context.toast(text: CharSequence): Unit defined in org.jetbrains.anko
public fun Context.toast(textResource: Int): Unit defined in org.jetbrains.anko
public inline fun AnkoContext<*>.toast(text: CharSequence): Unit defined in org.jetbrains.anko
public inline fun AnkoContext<*>.toast(textResource: Int): Unit defined in org.jetbrains.anko

I am pretty new to Kotlin and have been searching for a solution to this issue for a while. All help is appreciated.


Solution

  • Since the error says none are applicable you've probably imported a different Fragment class than the one defined by Android/Support Library.

    Check your imports.