简体   繁体   English

Kotlin - 类型不匹配:推断的类型是 Unit,但 Intent 是预期的

[英]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表达式为变量或属性赋值,则需要ifelse分支来计算所需的类型。 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.

相关问题 Kotlin:类型不匹配:推断的类型是 Intent? 但意图是预期的 - Kotlin : Type mismatch: inferred type is Intent? but Intent was expected Kotlin推断出类型不匹配 - Kotlin inferred type mismatch 类型不匹配:推断类型是字符串? 但预计字符串 kotlin - Type mismatch: inferred type is String? but String was expected kotlin 类型不匹配:推断类型是String,但是在kotlin中预计会出现Charset - Type mismatch: inferred type is String but Charset was expected in kotlin 类型不匹配:推断的类型是上下文? 但希望有上下文-Kotlin - Type mismatch: inferred type is Context? but Context was expected - Kotlin 类型不匹配:推断类型为 String 但预期为 Int,Kotlin - Type mismatch: inferred type is String but Int was expected , Kotlin Kotlin类型不匹配:推断类型是View! 但TextView是预料之中的 - Kotlin Type mismatch: inferred type is View! but TextView was expected 类型不匹配:推断的类型是但预期的 - Type mismatch: inferred type is but was expected 类型不匹配推断类型为 () -&gt; Unit 但 FlowCollector<int> 预计</int> - Type mismatch inferred type is () -> Unit but FlowCollector<Int> was expected 错误:类型不匹配:推断的类型是单位,但预期为布尔值 - error: Type mismatch: inferred type is Unit but Boolean was expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM