Search code examples
androidbuttonnosuchmethoderror

NoSuchMethod exception for Button onClick


I have an Android app that's testing a JNI library. The app has a button with an onClick handler:

<RelativeLayout
    ...
    <Button
        ...
        android:id="@+id/btnGenerate"           
        android:text="@string/btn_generate"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:onClick="btnGenerate_onClick"/>
</RelativeLayout>

Then, in the main activity:

public class MainActivity extends Activity {
    ...

    protected void btnGenerate_onClick(View view)
    {
        // Do some work...
        String str = <some result>; 

        final TextView lblNumbers = (TextView)findViewById(R.id.lblNumbers);
        if(lblNumbers != null)
        {
            lblNumbers.setText(str);
        }
    }
}

When I tap the button, I receive a NoSuchMethodException.

I lifted the solution from Android Button setOnClickListener Design Help, so I'm not sure why its not working.

Why is the code causing a NoSuchMethod exception?


09-05 07:20:57.417: W/dalvikvm(10563): threadid=1: thread exiting with uncaught exception (group=0x40aae228)
09-05 07:20:57.437: E/AndroidRuntime(10563): FATAL EXCEPTION: main
09-05 07:20:57.437: E/AndroidRuntime(10563): java.lang.IllegalStateException: Could not find a method btnGenerate_onClick(View) in the activity class com.cryptopp.prng.MainActivity for onClick handler on view class android.widget.Button with id 'btnGenerate'
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.view.View$1.onClick(View.java:3069)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.view.View.performClick(View.java:3549)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.view.View$PerformClick.run(View.java:14393)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.os.Handler.handleCallback(Handler.java:605)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.os.Looper.loop(Looper.java:154)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.app.ActivityThread.main(ActivityThread.java:4945)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at java.lang.reflect.Method.invokeNative(Native Method)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at java.lang.reflect.Method.invoke(Method.java:511)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at dalvik.system.NativeStart.main(Native Method)
09-05 07:20:57.437: E/AndroidRuntime(10563): Caused by: java.lang.NoSuchMethodException: btnGenerate_onClick [class android.view.View]
09-05 07:20:57.437: E/AndroidRuntime(10563):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at java.lang.Class.getMethod(Class.java:915)
09-05 07:20:57.437: E/AndroidRuntime(10563):    at android.view.View$1.onClick(View.java:3062)
09-05 07:20:57.437: E/AndroidRuntime(10563):    ... 11 more

Solution

  • The method should be public so that it can be seen from outside.

    Edit: Here is Android source code for handling onClick attribute - link

    In there you can see that it's trying to get the method you passed as a value for the attribute through getClass().getMethod() on the Context object.

    getMethod judgung by Oracle site "Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object", so the method must be public.