意外的令牌(使用';'来分隔同一行上的表达式)

Zha*_*hao 1 android kotlin

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

Log.d(TAG, msg:"onCreate called. Score is :$score")
Run Code Online (Sandbox Code Playgroud)

Zoe*_*Zoe 6

我假设你试图使用命名参数,判断我msg是第二个参数的名称Log.d,并在代码中匹配.但是,你有两个问题:

  • 命名参数仅适用于所有Kotlin代码.如果函数是Java,则无法使用它
  • 命名参数使用=,而不是:

就像你可以做的那样:

data class SomeClass(val x: String, val y: String)
fun someFunction(){
    SomeClass(y = "y", x = "x")
}
Run Code Online (Sandbox Code Playgroud)

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

Log.d(TAG, msg="onCreate called. Score is :$score")
Run Code Online (Sandbox Code Playgroud)

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