Kotlin error: Expected 2 parameters of types android.widget.CompoundButton!, kotlin.Boolean
Kotlin error: Expected 2 parameters of types android.widget.CompoundButton!, kotlin.Boolean
我有一个 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!,
(表示“意外标记”)
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:
) class。
试试这个:
alarmSwitch.setOnCheckedChangeListener(object:OnCheckedChangeListener() {
fun onCheckedChanged(buttonView:CompoundButton, isChecked:Boolean) {
// whatever...
}
})
希望对您有所帮助
我们还可以使用 lambda 表达式简化代码
alarmSwitch.setOnCheckedChangeListener { buttonView, isChecked -> /* whatever...*/ }
https://antonioleiva.com/functional-programming-android-kotlin-lambdas/
checkboxQuickApproval.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
if (isChecked) {
viewModel.onChangeCheckboxValue(isChecked)
}
}
})
我有一个 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!,
(表示“意外标记”)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:
) class。
试试这个:
alarmSwitch.setOnCheckedChangeListener(object:OnCheckedChangeListener() {
fun onCheckedChanged(buttonView:CompoundButton, isChecked:Boolean) {
// whatever...
}
})
希望对您有所帮助
我们还可以使用 lambda 表达式简化代码
alarmSwitch.setOnCheckedChangeListener { buttonView, isChecked -> /* whatever...*/ }
https://antonioleiva.com/functional-programming-android-kotlin-lambdas/
checkboxQuickApproval.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
if (isChecked) {
viewModel.onChangeCheckboxValue(isChecked)
}
}
})