'receiver type mismatch' with Fragment and Anko toast

蓝咒 提交于 2019-12-11 04:18:37

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/44414041/receiver-type-mismatch-with-fragment-and-anko-toast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!