【问题标题】:java.lang.illegalstateexception could not find method in a parent or ancestor contextjava.lang.illegalstateexception 在父上下文或祖先上下文中找不到方法
【发布时间】:2021-04-14 06:37:07
【问题描述】:

" 我正在制作一个 tictactoe 应用程序。我正在尝试制作 onClick 按钮,但是 它显示了这样的错误,应用程序正在崩溃。”

这是我的 kotlin 代码

fun btnclick(view:View) {                               
        val btn=view as Button
        var cell = 0
        when (btn.id) {
            R.id.button1 -> cell = 1
            R.id.button2 -> cell = 2
            R.id.button3 -> cell = 3
            R.id.button4 -> cell = 4
            R.id.button5 -> cell = 5
            R.id.button6 -> cell = 6
            R.id.button7 -> cell = 7
            R.id.button8 -> cell = 8
            R.id.button9 -> cell = 9
        }
        playgame(cell, btn)
    }

这里是xml代码

                <Button
                    android:id="@+id/button1"
                    android:layout_width="45pt"
                    android:layout_height="45pt"
                    android:onClick="btnclick"
                     />
                <Button
                    android:id="@+id/button2"
                    android:layout_width="45pt"
                    android:layout_height="45pt"                    
                    android:onClick="btnclick"
                    android:textSize="45sp" />
                <Button
                    android:id="@+id/button3"
                    android:layout_width="45pt"
                    android:onClick="btnclick"
                    android:textSize="45sp" />
...so on

【问题讨论】:

  • 发布你的日志
  • 我不知道 kotlin。但是,错误消息说它找不到 id button5。你的ids 看起来像android:id="@+id/button5"。我很想说"button5" != "@+id/button5"。对吗?
  • nono button5 与“@+id/button5”匹配。它显示了一些关于 View 类的错误。但我不知道它是什么

标签: android kotlin onclick


【解决方案1】:

屏幕截图显示点击处理程序签名采用Button 参数。

框架寻找一个接受View参数的方法。

改变

fun btnclick(view:Button)

fun btnclick(view: View?)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多