【问题标题】:Unexpected tokens (use ';' to separate expressions on the same line)意外标记(使用 ';' 分隔同一行上的表达式)
【发布时间】:2018-09-07 18:36:05
【问题描述】:

我不知道为什么我在写这个 kotlin 代码时会遇到这样的问题

Log.d(TAG, msg:"onCreate called. Score is :$score")

【问题讨论】:

  • 删除 "msg:" -> Log.d(TAG, "onCreate called. Score is :$score")

标签: android kotlin


【解决方案1】:

我假设您尝试使用命名参数,判断我的 msgLog.d 中第二个参数的名称,并且它与您的代码匹配。但是,您确实有两个问题:

  • 命名参数仅适用于全 Kotlin 代码。如果该函数是 Java 语言,则不能使用它
  • 命名参数使用=,而不是:

你可以这样做:

data class SomeClass(val x: String, val y: String)
fun someFunction(){
    SomeClass(y = "y", x = "x")
}

但你不能为Log.d 这样做,因为它不是 Kotlin 函数。作为参考,这是适当的语法:

Log.d(TAG, msg="onCreate called. Score is :$score")

但它不会编译,因为Named arguments are not allowed for non-Kotlin functions。所以删除它。您不能在该方法中使用命名参数。

【讨论】:

    【解决方案2】:

    去掉“味精:”

    Log.d(TAG, "onCreate called. Score is :$score")
    

    【讨论】:

      【解决方案3】:

      尝试使用 Log.d(TAG, "msg:onCreate called. Score is :$score")

      【讨论】:

        猜你喜欢
        • 2017-10-28
        • 2020-06-28
        • 2018-01-15
        • 2019-08-04
        • 1970-01-01
        • 2018-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多