gpt4 book ai didi

android - Unresolved 对 Kotlin Android 中函数的引用

转载 作者:行者123 更新时间:2023-11-30 00:16:37 25 4
gpt4 key购买 nike

我的 MainActivity 中有以下 onCreate 函数。

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

var selected_answer = 0

val btn_list = mutableListOf<ToggleButton>(button1, button2, button3, button4)

for(i in btn_list){
i.setOnClickListener{
toggle_rest(btn_list.indexOf(i), btn_list)
selected_answer = btn_list.indexOf(i)+1

}
}


fun toggle_rest(toCheck : Int, btn_list : List<ToggleButton>){


for (i in btn_list) {
i.setChecked(btn_list.indexOf(i) == toCheck)

}
}
question_txt.setText("Quiz will begin shortly!")
}

当我调用 toggle_rest 时出现错误 toggle_rest:Unresolved reference

最佳答案

在你的代码中使用之前你必须定义toggle_rest函数,否则你会得到unresolve reference编译错误:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

var selected_answer = 0

//Declare your function before you use
fun toggle_rest(toCheck : Int, btn_list : List<ToggleButton>){
for (i in btn_list) {
i.setChecked(btn_list.indexOf(i) == toCheck)
}
}

val btn_list = mutableListOf<ToggleButton>(button1, button2, button3, button4)

for(i in btn_list){
i.setOnClickListener{
toggle_rest(btn_list.indexOf(i), btn_list)
selected_answer = btn_list.indexOf(i)+1
}
}

question_txt.setText("Quiz will begin shortly!")
}

关于android - Unresolved 对 Kotlin Android 中函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47135189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com