为什么在以 XML 命名 onClick 并在 Main Activity 上调用它之后,在 XML onClick 行上出现错误“未找到相应的方法处理程序”?
来自 activity_main.xml 的 fragment :
<Button
android:id="@+id/buPercentage"
android:layout_width="30pt"
android:layout_height="30pt"
android:layout_weight="1"
android:background="@color/lightgray"
android:onClick="buPercentageEvent" //Error says Corresponding method handler not found
android:text="%"
android:textSize="18sp" />
来自 MainActivity.kt 的 fragment
fun buPercentageEvent()
{
val convertToDecimal = displayText.text.toString()
val newNumber = convertToDecimal.toDouble() / 100
displayText.setText(newNumber.toString())
}
最佳答案
这样做
fun test(view: View){
Log.e("Click","Yes")
}
这会起作用。
关于Android Studio XML 错误 "Corresponding method handler not found"onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44917962/