[英]Kotlin - Type mismatch: inferred type is Unit but Intent was expected
I'm very new to Kotlin and I'm getting an error stating Kotlin - Type mismatch: inferred type is Unit but Intent was expected
and I'm unsure how to resolve this.我对 Kotlin 非常陌生,我收到一条错误消息,指出Kotlin - Type mismatch: inferred type is Unit but Intent was expected
,我不确定如何解决这个问题。
Any suggestions are appreciated.任何建议表示赞赏。
Code Snippet:代码片段:
val webURL: String? = dataMap["uri"]
val intent: Intent
intent = if (!webURL.isNullOrEmpty()) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(webURL))
intent.data = Uri.parse(webURL)
} else {
Intent(this, MainActivity::class.java)
}
Note:笔记:
The error occurs on the line:错误发生在这一行:
intent.data = Uri.parse(deeplinkURL)
val webURL: String? = dataMap["uri"]
val intent: Intent
intent = if (!webURL.isNullOrEmpty()) {
Intent(Intent.ACTION_VIEW, Uri.parse(webURL))
} else {
Intent(this, MainActivity::class.java)
}
If you are going to assign a value to a variable or property from an if
/ else
expression, you need both the if
and the else
branches to evaluate to the desired type.如果要从if
/ else
表达式为变量或属性赋值,则需要if
和else
分支来计算所需的类型。 And, you do not need to provide the Uri
to the Intent
twice.而且,您不需要两次向Intent
提供Uri
。
如果条件为真,则不返回任何内容,只需在 else 之前添加intent
行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.