Converting Java To Kotlin: Type mismatch. Required: MenuSlidingTabStrip.OnTabSelectedListener? Found: (Nothing, Nothing) → Boolean

huangapple go评论82阅读模式
英文:

Converting Java To Kotlin: Type mismatch. Required: MenuSlidingTabStrip.OnTabSelectedListener? Found: (Nothing, Nothing) → Boolean

问题

我正在将一款最初用Java编写的Android应用转换为Kotlin。
我正在努力理解以下错误消息:

类型不匹配。
所需:
MenuSlidingTabStrip.OnTabSelectedListener?
找到:
(Nothing,Nothing) → Boolean

以下是发出错误信号的代码片段(在转换之前,它完全正常运行):

private var tabs: MenuSlidingTabStrip? = null //Kotlin化后的类

tabs!!.setOnTabSelectedListner{ tab, category -> /*类型不匹配...*/
    listView!!.post {
    ...
    }
}

在将此Java代码(位于MenuSlidingTabStrip中)转换为Kotlin后出现了问题:

public void setOnTabSelectedListner(OnTabSelectedListener listener) {
    this.listener = listener;
}

public interface OnTabSelectedListener {
    public void OnTabSelected(View tab, MenuCategory category);
}

转换为Kotlin:

fun setOnTabSelectedListner(listener: OnTabSelectedListener?) {
    this.listener = listener
}

interface OnTabSelectedListener {
    fun onTabSelected(tab: View?, category: MenuCategory?)
}

你能看出问题吗?需要更多的代码吗?

英文:

I am converting an Android app that was originally written in Java to Kotlin.
I am struggling to understand the following error message:

> Type mismatch.
Required:
MenuSlidingTabStrip.OnTabSelectedListener?
Found:
(Nothing, Nothing) → Boolean

Here is the fragment of code where the error is being signalled (and it was working perfectly fine before the conversion):

private var tabs: MenuSlidingTabStrip? = null //The Kotlinized class

        tabs!!.setOnTabSelectedListner{ tab, category -> /*Type mismatch...*/
            listView!!.post {
            ...
            }
        }

The issue arose after converting this Java code (found in MenuSlidingTabStrip) :

public void setOnTabSelectedListner(OnTabSelectedListener listener) {
	this.listener = listener;
}

public interface OnTabSelectedListener {
	public void OnTabSelected(View tab, MenuCategory category);

}

To Kotlin

  fun setOnTabSelectedListner(listener: OnTabSelectedListener?) {
    this.listener = listener
}

interface OnTabSelectedListener {
    fun onTabSelected(tab: View?, category: MenuCategory?)
}

Can you see the issue? Do you need more code?

答案1

得分: 2

我建议您使用本地支持的 lambda 表达式,如下所示:

// 使 `this.listener` 的外观也像 lambda 签名一样
fun setOnTabSelectedListner(listener: (tab: View?, category: MenuCategory?) -> Unit) {
    this.listener = listener
}

tabs!!.setOnTabSelectedListner { tab, category ->
    // ...
}

如果您仍然希望使用手动定义的接口进行 SAM 转换,那么可以根据 Kotlin 1.4-M1 发布说明 中的描述,使用 fun 关键字标记该接口(仅适用于 Kotlin 1.4):

fun interface OnTabSelectedListener {
    fun onTabSelected(tab: View?, category: MenuCategory?)
}

// 使用以下方式调用该方法:
tabs!!.setOnTabSelectedListner(OnTabSelectedListener { tab, category ->
    // ...
})

如果您尚未切换到 Kotlin 1.4(仍处于 beta 阶段),并且不愿意使用本地提供的 lambda 语法,则必须手动实例化匿名对象:

tabs!!.setOnTabSelectedListner(object : OnTabSelectedListener {
    override fun onTabSelected(tab: View?, category: MenuCategory?) {
        // ...
    }
})
英文:

I'd suggest you to use the natively supported lambdas as:

// make `this.listener` look like lambda signature as well
fun setOnTabSelectedListner(listener: (tab: View?, category: MenuCategory?) -> Unit) {
    this.listener = listener
}

tabs!!.setOnTabSelectedListner { tab, category ->
    // ...
}

If you still wanna use the SAM conversion with the manually defined interface, then mark the interface with the fun keyword as described in Kotlin 1.4-M1 Release changelog (will only work with Kotlin 1.4):

fun interface OnTabSelectedListener {
    fun onTabSelected(tab: View?, category: MenuCategory?)
}

// call that method like this:
tabs!!.setOnTabSelectedListner(OnTabSelectedListener { tab, category ->
    // ...
})

If you've not switched to Kotlin-1.4 (which is still in beta state), you have to manually instantiate the anonymous object if you are not willing to use the natively provided lambda syntax:

tabs!!.setOnTabSelectedListner(object : OnTabSelectedListener {
    override fun onTabSelected(tab: View?, category: MenuCategory?) {
        // ...
    }
})

huangapple
  • 本文由 发表于 2020年7月24日 01:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63059867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定