gpt4 book ai didi

kotlin - 如何在 kotlin 中调用接口(interface)?

转载 作者:行者123 更新时间:2023-12-02 13:21:12 28 4
gpt4 key购买 nike

我的工作中没有项目,他们要求我给我通过,但是在通过整个项目后,有一部分现在给了我一个代码错误。显然这是我第一次使用 Kotlin,我不知道,但我确实有一个想法。我试图解决它,但我没有成功。所以我在寻求帮助。我在开始时收到一个错误

= SpeechService.Lintener {



这里的代码
private val mSpeechServiceListener = SpeechService.Listener { text: String?, isFinal: Boolean ->
if (isFinal) {
mVoiceRecorder!!.dismiss()
}

if (mText != null && !TextUtils.isEmpty(text)) {
runOnUiThread {
if (isFinal) {

if (mText!!.text.toString().equals("hola", ignoreCase = true) || b == true) {
if (b == true) {
mText!!.text = null
mTextMod!!.text = text

repro().onPostExecute(text)
random = 2
} else {
b = true
mText!!.text = null
val saludo = "Bienvenido, ¿que desea?"
mTextMod!!.text = saludo
repro().onPostExecute(saludo)
}
}

} else {
mText!!.text = text
}
}
}
}

这里是界面
interface Listener {

fun onSpeechRecognized(text: String?, isFinal: Boolean)

}

请帮我。错误是“接口(interface)监听器没有构造函数”

最佳答案

SpeechService.Listener { } SAM 接口(interface)的语法只有在接口(interface)是用 Java 编写时才有可能(参见 https://kotlinlang.org/docs/reference/java-interop.html#sam-conversions)。因为接口(interface)是用 Kotlin 编写的,所以你必须这样写:

private val mSpeechServiceListener = object : SpeechService.Listener {

override fun onSpeechRecognized(text: String?, isFinal: Boolean) {
// Code here
}

}

你真的不需要 SpeechService.Listener虽然在 Kotlin 中的界面。您可以只使用 lambda 函数。这取决于接口(interface)是来自库还是您自己编写的。
private val mSpeechServiceListener: (String?, Boolean) -> Unit = { text, isFinal ->
// Code here
}

关于kotlin - 如何在 kotlin 中调用接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56557625/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com