我有一个 Kotlin 错误提示
Expected 2 parameters of types android.widget.CompoundButton!, kotlin.Boolean
红色波浪线位于下面代码中的第一个 {
:
alarmSwitch.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener()
{
fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean)
{
我试过:
buttonView: !CompoundButton,
(表示“期望逗号或)”)buttonView: CompoundButton!,
(显示“意外 token ”)buttonView!: CompoundButton,
(表示“期望逗号或)”)!buttonView: CompoundButton,
(表示“期望逗号或)”)buttonView: CompoundButton?,
(表示“预期 2 个类型为 android.widget.CompoundButton 的参数!,kotlin.Boolean”)
官方 Kotlin 文档说:
Notation for Platform Types
As mentioned above, platform types cannot be mentioned explicitly in the program, so there’s no syntax for them in the language. Nevertheless, the compiler and IDE need to display them sometimes (in error messages, parameter info etc), so we have a mnemonic notation for them:
T! means “T or T?”,
(Mutable)Collection! means “Java collection of T may be mutable or not, may be nullable or not”,
Array<(out) T>! means “Java array of T (or a subtype of T), nullable or not”
我不完全理解文档在说什么。我该如何解决这个错误?
最佳答案
我认为您需要创建匿名类对象所需的对象关键字 (object:
)。
试试这个:
alarmSwitch.setOnCheckedChangeListener(object:OnCheckedChangeListener() {
fun onCheckedChanged(buttonView:CompoundButton, isChecked:Boolean) {
// whatever...
}
})
希望对你有帮助
关于Kotlin 错误 : Expected 2 parameters of types android. widget.CompoundButton!, kotlin.Boolean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146508/