NoSuchMethod exception for Button onClick

jww

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
cliffroot

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NoSuchMethod Exception

From Dev

Android UIAutomation Instrumentation NoSuchMethod Exception

From Dev

Solr custom filter NoSuchMethod exception

From Dev

NullPointer Exception on Onclick of Button in FragmentActivity android

From Dev

What's this NoSuchMethod exception using Retrofit with Observables?

From Dev

NoSuchMethod exception using google endpoints + JDO

From Dev

NoSuchMethod exception using google endpoints + JDO

From Dev

Spring 4.3.1 + Hibernate 5.2.1 creating SessionFactoryImplementor.getProperties noSuchMethod exception

From Dev

NoSuchMethod exception in Flink when using dataset with custom object array

From Dev

Spring 4.3.1 + Hibernate 5.2.1 creating SessionFactoryImplementor.getProperties noSuchMethod exception

From Dev

Get Line Highlighting (or at Least a Stack Trace) for noSuchMethod Exception Dart

From Dev

NoSuchMethod exception in Flink when using dataset with custom object array

From Dev

Android - Null Exception Pointer on my button's onclick. Works on emulator but not device

From Dev

Android Put intent from onclick of button created programmatically - Activity Not Found Exception

From Dev

BodyEditorLoader - noSuchMethod

From Dev

Javascript onclick button change

From Dev

Radio button onclick not working

From Dev

Onclick button Event in fragment

From Dev

redirect using button onclick

From Dev

Button OnClick() Event not Firing

From Dev

Button not responding to OnClick

From Dev

NullPointerException on SharedPreferences in Button OnClick

From Dev

Button onclick refreshing page

From Dev

HTML button onclick event

From Dev

OnClick for Floating Action Button

From Dev

onClick with CTRL button

From Dev

Xamarin Forms Button OnClick

From Dev

Changing button image onclick

From Dev

onclick event on submit button

Related Related

  1. 1

    NoSuchMethod Exception

  2. 2

    Android UIAutomation Instrumentation NoSuchMethod Exception

  3. 3

    Solr custom filter NoSuchMethod exception

  4. 4

    NullPointer Exception on Onclick of Button in FragmentActivity android

  5. 5

    What's this NoSuchMethod exception using Retrofit with Observables?

  6. 6

    NoSuchMethod exception using google endpoints + JDO

  7. 7

    NoSuchMethod exception using google endpoints + JDO

  8. 8

    Spring 4.3.1 + Hibernate 5.2.1 creating SessionFactoryImplementor.getProperties noSuchMethod exception

  9. 9

    NoSuchMethod exception in Flink when using dataset with custom object array

  10. 10

    Spring 4.3.1 + Hibernate 5.2.1 creating SessionFactoryImplementor.getProperties noSuchMethod exception

  11. 11

    Get Line Highlighting (or at Least a Stack Trace) for noSuchMethod Exception Dart

  12. 12

    NoSuchMethod exception in Flink when using dataset with custom object array

  13. 13

    Android - Null Exception Pointer on my button's onclick. Works on emulator but not device

  14. 14

    Android Put intent from onclick of button created programmatically - Activity Not Found Exception

  15. 15

    BodyEditorLoader - noSuchMethod

  16. 16

    Javascript onclick button change

  17. 17

    Radio button onclick not working

  18. 18

    Onclick button Event in fragment

  19. 19

    redirect using button onclick

  20. 20

    Button OnClick() Event not Firing

  21. 21

    Button not responding to OnClick

  22. 22

    NullPointerException on SharedPreferences in Button OnClick

  23. 23

    Button onclick refreshing page

  24. 24

    HTML button onclick event

  25. 25

    OnClick for Floating Action Button

  26. 26

    onClick with CTRL button

  27. 27

    Xamarin Forms Button OnClick

  28. 28

    Changing button image onclick

  29. 29

    onclick event on submit button

HotTag

Archive