【问题标题】:kotlin unresolved reference none of the following candidates is applicable because of receiver type mismatchkotlin unresolved reference 由于接收器类型不匹配,以下候选者均不适用
【发布时间】:2021-05-11 01:11:00
【问题描述】:

Utils 类中的HandleAPIError。为 API 错误创建 Snackbar 的自定义方法。

未解决的参考。由于接收器类型不匹配,以下候选均不适用: public fun View.snackbar(message: String, action: (() → Unit)? = ...): Unit 在文件 Utils.kt 中定义

requireview() 中 Fragment 中的接收者类型。用于活动的接收器类型。

Utils.kt

fun View.snackbar(message: String, action: (() -> Unit)? = null){
    val snackbar = Snackbar.make(this, message, Snackbar.LENGTH_LONG)
    action?.let {
        snackbar.setAction("Retry"){
            it()
        }
        snackbar.show()
    }
}

fun Fragment.handleApiError(
    failure : Resource.Failure,
    retry : (() -> Unit)? = null
    ){
    when{
        failure.isNetworkError -> requireView().snackbar("Please check internet Connection", retry )
    }
}

fun Activity.handleApiError(
    failure : Resource.Failure,
    retry : (() -> Unit)? = null
){
    when{
        //which receiver type used for snackbar?
        failure.isNetworkError -> snackbar(
            "Please check internet Connection",
            retry
        )
}
}

【问题讨论】:

  • 你解决了吗?我有一个示例问题,你能帮我吗?

标签: kotlin android-activity view snackbar


【解决方案1】:

尝试将this.window.decorView.rootView 用作:

fun Activity.handleApiError(
        failure : Resource.Failure,
        retry : (() -> Unit)? = null
){
    when{
        //which receiver type used for snackbar?
        failure.isNetworkError -> this.window.decorView.rootView.snackbar(
                "Please check internet Connection",
                retry
        )
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2019-04-19
    • 2022-06-12
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多