简体   繁体   English

赋值不是表达式,在这种情况下只允许使用表达式 - Kotlin

[英]Assignments are not expressions, and only expressions are allowed in this context - Kotlin

bindingSub.btnCoinup.setOnClickListener {
    fbFirestore?.collection("users")?.document(fbAuth?.uid.toString())?.get()?.addOnSuccessListener { document ->
        if (document != null) {
            var autoCoin = document.data?.values.toString().replace("[", "").replace("]", "").toLong()
            bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}")
        } else {
            bindingSub.tvAutoCoin.setText("Coin: 0")
        }
    } ?.addOnFailureListener { exception ->
        ...

bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}") bindingSub.tvAutoCoin.setText("硬币:${autoCoin += 1}")

I tried really hard to fix this error, but I couldn't fix it.我非常努力地修复了这个错误,但我无法修复它。

Even if the code is dirty, I would appreciate it if you understand...即使代码很脏,如果您理解,我将不胜感激...

You are assigning a new value in this line to the autoCoin variable:您在这一行中为autoCoin变量分配了一个新值:

bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}")

String templates only allow expressions.字符串模板只允许表达式。 An expression is something that results in a value of some type, even if that type is Unit and can be on the right hand side of an assignment statement (eg val x = 3 assigns the value of the expression 3 to x ).表达式是产生某种类型值的东西,即使该类型是 Unit 并且可以位于赋值语句的右侧(例如val x = 3将表达式3的值赋给x )。 Some languages (like Java or C++) assignments are expressions, but in Kotlin they are not.某些语言(如 Java 或 C++)赋值是表达式,但在 Kotlin 中它们不是。

The x += 1 operator is a special shorthand syntax for x = x + 1 . x += 1运算符是x = x + 1的特殊速记语法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分配不是表达式,在这种情况下只能使用表达式-Kotlin - Assignments are not expressions and only expressions are allowed in this context - Kotlin if 语句:赋值不是表达式,在这个上下文中只允许表达式 kotlin - If statement: Assignments are not expressions, and only expressions are allowed in this context kotlin Android Studio 错误“赋值不是表达式,在此上下文中只允许使用表达式。” - Android Studio Error “Assignments are not expressions, and only expressions are allowed in this context.” 此语言级别不允许使用Android Studio Lambda表达式 - Android Studio Lambda expressions are not allowed at this language level Kotlin (Android Studio) 意外标记(使用“;”分隔同一行上的表达式) - Kotlin (Android Studio) Unexpected tokens (use ';' to separate expressions on the same line) 错误:“只允许 Kotlin 标准库使用‘kotlin’包” - Error: "Only the Kotlin standard library is allowed to use the 'kotlin' package" 使用Lambda表达式和Butterknife - Use Lambda expressions and Butterknife 如何在Android Studio中自动替换为Lambda表达式 - How to auto replace into lambda expressions in Android Studio 如何将代数表达式显示为字符串? - How can I display algebraic expressions as a String? 错误:-source 1.7不支持lambda表达式(使用-source 8或更高版本来启用lambda表达式) - Error: lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM