others-how to solve IllegalStateException when trying to click a button in android?

1. Purpose

In this post, I will show you how to solve java.lang.IllegalStateException: Could not find method xxx(View) in a parent or ancestor Context for android:onClick attribute defined on view class exception when trying to click a view on android:


2023-04-21 10:52:38.566 26207-26207/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bswen.app, PID: 26207
    java.lang.IllegalStateException: Could not find method testOnGoingService(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.TextView with id 'btnTestOnGoingService'
        at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:5406)
        at android.view.View$DeclaredOnClickListener.onClick(View.java:5365)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Here is the layout xml file content:

        <TextView
            android:id="@+id/btnTestOnGoingService"
            android:text="test service"
            android:visibility="visible"
            android:layout_marginStart="10dp"
            android:onClick="testOnGoingService"
            android:clickable="true"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/layout_background_green"
            style="@style/HeaderTitleTextWhite"
            android:layout_width="wrap_content"
            android:gravity="center"
            android:layout_height="wrap_content"/>

The purpose of this view is very clear, it can be clicked and the testOngoingService method will be invoked when user click it.

Here is the method code:

    public void testOnGoingService() {
        App app = (App)getApplication();
        app.startService(this);
    }

But when I click the button, I got this error:

2023-04-21 10:52:38.566 26207-26207/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bswen.app, PID: 26207
    java.lang.IllegalStateException: Could not find method testOnGoingService(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.TextView with id 'btnTestOnGoingService'

It seems that android can not find the method for the onClick event, but it does exist!!!

Why?



2. Solution

The fix for this problem is very simple, just ajust your method as follows:

    public void testOnGoingService(View view) {
        App app = (App)getApplication();
        app.startService(this);
    }

The only difference between this and the old one is that it has a parameter View view, that is the key point, it must conform to the constraint of android system, it must has a View parameter, otherwise, android can not find it.

So ,to summarize, the error occurred because: the onClick expects a method with a View parameter.



3. Summary

In this post, I demonstrated how to solve the IllegalStateException on android system, the key point is to make your onClick method to conform to the standard of android. That’s it, thanks for your reading.