Unable to bind onClick xml onto a method in my activity

Jimmy picture Jimmy · Nov 22, 2010 · Viewed 11.5k times · Source

I've got the following Button declared in my main.xml

<Button android:name="@+id/clickedStartService"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="clickedStartService"
        android:text="Start"
        />
<Button android:name="@+id/clickedStopService"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="clickedStopService"
        android:text="Stop"
        />

And I have the following methods in the activity..

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
public void clickedStartService(View v)
    {
        startService(new Intent(this, LocalService.class));
    }

    public void clickedStopService(View v)
    {
        stopService(new Intent(this, LocalService.class));
    }

I'm getting the following error when I click the button. I have tried rebuilding the project in my IDE and have also restarted the emulator, but same error re-appears. I've followed the documentation on the Android developers site, and I've had this working in a separate project, I just can't get it to work here..

ERROR/AndroidRuntime(335): FATAL EXCEPTION: main
        java.lang.IllegalStateException: Could not find a method clickedStartService(View) in the activity class com.jameselsey.observerpattern.MyApp for onClick handler on view class android.widget.Button
        at android.view.View$1.onClick(View.java:2059)
        at android.view.View.performClick(View.java:2408)
        at android.view.View$PerformClick.run(View.java:8816)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NoSuchMethodException
        at java.lang.Class.getDeclaredMethods(Native Method)
        at java.lang.ClassCache.getDeclaredPublicMethods(ClassCache.java:166)
        at java.lang.ClassCache.getDeclaredMethods(ClassCache.java:179)
        at java.lang.ClassCache.findAllMethods(ClassCache.java:249)
        at java.lang.ClassCache.getFullListOfMethods(ClassCache.java:223)
        at java.lang.ClassCache.getAllPublicMethods(ClassCache.java:204)
        at java.lang.Class.getMethod(Class.java:984)
        at android.view.View$1.onClick(View.java:2052)
        ... 11 more

Answer

Alexander Lucas picture Alexander Lucas · Nov 22, 2010

XML click listeners were added in Android 1.6. Check your AndroidManifest to verify that your min SDK version isn't set to cupcake (1.5, or API level 3) -

If you're using Eclipse with the ADT plugin, also check project properties > Android and check the build target, make sure it's Donut or higher.