Amr
Amr

Reputation: 11

How to reference a method inside a button click

I am learning kotlin. i set button click handler in the xml as shown below in the code..when the button is clicked i call the method beginSearch(), but then the App crash and i receive the below posted error.

please let me know how to fix this error.

Activity

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

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

button:

<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"/>

error:

    --------- 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)

Upvotes: 1

Views: 2219

Answers (4)

Ashish
Ashish

Reputation: 6919

Just use

btnCheckSearchResult.setOnClickListener {
              beginSearch("Trump")
            }

or

btnCheckSearchResult.setOnClickListener({
            beginSearch("Trump")
        })

Upvotes: 3

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

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'

Remove android:onClick="clickHandler" from XML

And try with

btnCheckSearchResult.setOnClickListener {
              beginSearch("Trump")
            }

And

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

Upvotes: 4

Jasurbek
Jasurbek

Reputation: 2966

make your function public

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

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

Upvotes: 2

Chrisvin Jem
Chrisvin Jem

Reputation: 4060

Function must be public (or protected).

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

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

P.S - You could also use setOnClickListener as the other answers have pointed out.

Upvotes: 3

Related Questions