如何在按钮单击内引用方法

时间:2019-07-09 13:09:41

标签: android kotlin android-button

我正在学习科特林。我在xml中设置了按钮单击处理程序,如下代码所示。.单击按钮时,我调用方法beginSearch(),但随后应用崩溃,我收到了以下内容 错误。

请让我知道如何解决此错误。

活动

private fun clickHandler(v : View?) : Unit {

    when(v?.id) {
        R.id.btnCheckSearchResult -> beginSearch("Trump")
    }
}

按钮

<Button
        android:id="@+id/btnCheckSearchResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Check Search Result"
        android:layout_below="@id/etSearchEntry"
        android:onClick="clickHandler"/>

错误

    --------- beginning of crash
    2019-07-09 15:02:06.146 14883-14883/com.example.retrofitkotlin_v10 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.retrofitkotlin_v10, PID: 14883
    java.lang.IllegalStateException: Could not find method clickHandler(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnCheckSearchResult'
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

4 个答案:

答案 0 :(得分:2)

  

java.lang.IllegalStateException:找不到方法   android:onClick的父级或祖先上下文中的clickHandler(View)   在视图类上定义的属性   具有ID的android.support.v7.widget.AppCompatButton   'btnCheckSearchResult'

从XML中删除 android:onClick="clickHandler"

并尝试

btnCheckSearchResult.setOnClickListener {
              beginSearch("Trump")
            }

import kotlinx.android.synthetic.main.your_layout.*

答案 1 :(得分:2)

只需使用

Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLWebsocketServlet' defined in class path resource 
[com/oembedler/moon/graphql/boot/GraphQLWebsocketAutoConfiguration.c
lass]: Unsatisfied dependency expressed through method 'graphQLWebsocketServlet' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'graphql.servlet.GraphQLInvocationInputFactory' available: expected at least 1 bean which qualifies as autowire candidate. 

btnCheckSearchResult.setOnClickListener {
              beginSearch("Trump")
            }

答案 2 :(得分:2)

该功能必须是公开的(或受保护的)。

public fun clickHandler(v : View?) : Unit {

    when(v?.id) {
        R.id.btnCheckSearchResult -> beginSearch("Trump")
    }
}

PS-您也可以使用setOnClickListener作为其他答案指出。

答案 3 :(得分:1)

使您的功能public

public fun clickHandler(v : View?) : Unit {

    when(v?.id) {
        R.id.btnCheckSearchResult -> beginSearch("Trump")
    }
}