【问题标题】:Type mismatch: inferred type is Unit but Boolean was expected类型不匹配:推断类型为 Unit 但预期为 Boolean
【发布时间】:2019-11-19 01:51:49
【问题描述】:

我的手势错误,这是我的代码错误,startActivity(intent)Toast.makeText 中的错误

R.id.menu_share -> {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
    startActivity(intent)
}
R.id.menu_info -> {
    Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
}
else -> false

【问题讨论】:

  • startActivity(intent)Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show() 都返回 Unit 类型,而在您的 else 语句中,您返回的是 false,即 Boolean。您是否在 else 语句中使用 Boolean 值?您可能应该考虑将其删除。

标签: kotlin


【解决方案1】:

我们需要更多代码才能给你一个完整的答案,但我可以尝试假设并给你最接近的答案。

when有两种使用方式

  1. 独立
  2. 作为表达式

如果您将其用作switch,即根据具体情况执行不同的操作 您不需要返回值,也不需要else 语句

例如:

when (menuItem.id) { /** I guess you're trying to perform differet actions based on menu item click */
    R.id.menu_share -> {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
        startActivity(intent)
    } /** returns Unit */

    R.id.menu_info -> {
        Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
    } /** returns Unit */

} /** result ignores / Unit */

您使用when 的另一种方式是作为表达式,此时您希望语句返回一个值。

在这种情况下,您必须为您提供的类型填写所有可能的情况,如果类型是您无法验证所有其他选项的类型,则必须填写 else,例如 IntString

例如:

val result = when (menuItem.id) { /** I guess you're trying to perform different actions based on menu item click */
    R.id.menu_share -> {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
        startActivity(intent)
    } /** Type mismatch: inferred type is Unit but Boolean was expected */

    R.id.menu_info -> {
        Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
    } /** Type mismatch: inferred type is Unit but Boolean was expected */

    else -> false /** returns Boolean */

} /** Type mismatch: inferred type is Unit but Boolean was expected */

要解决此问题,您需要在所有情况下都返回相同的类型

val result = when (menuItem.id) { /** I guess you're trying to perform different actions based on menu item click */
    R.id.menu_share -> {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
        startActivity(intent)
        true
    } /** returns Boolean */

    R.id.menu_info -> {
        Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
        true
    } /** returns Boolean */

    else -> false /** returns Boolean */

} /** returns Boolean */

希望我的解释能回答你的问题,如果没有,欢迎你发表评论。

【讨论】:

    【解决方案2】:
    R.id.menu_share -> {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web.whatsapp.com"))
        startActivity(intent)
    }
    R.id.menu_info -> {
        Toast.makeText(this,"Ada Toast", Toast.LENGTH_LONG).show()
    }
    else -> {
        // you can either left this block empty or just put `return`
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多