Search code examples
androidonclickandroid-5.0-lollipop

Could not find a method onClick_Foo(View) - first time running on Android Lollipop


I have an app that is about a year old, is on the Play store in beta, has gone through dozens of revisions. All of a sudden I'm getting an error:

Could not find a method onClick_Foo(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'Foo_Button'

I'm getting this error on every one of the 7 buttons defined in my XML. Since yesterday I've updating appcompat-v7 from 21.0.3 to 22.0.0 but also upgraded my testing device from KitKat to Lollipop for the first time.

I've double-checked spellings, capitalizations, none of the usual suspects explains this. Here's a sample of the relevant code. Let me know if you feel more would be helpful. (The activity has 915 lines of code and the xml 186, so don't ask for the whole thing). Testing on a Verizon Note 4 running Lollipop 5.0.1

activity_pick.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:theme="@style/AppTheme"
    tools:context="com.myapp.Pick"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Ratings_Button"
            android:textSize="16dp"
            android:text="@string/Pick_Ratings_Button"
            android:onClick="onClick_Ratings"
            android:background="@android:drawable/btn_default"/>
</LinearLayout>
</ScrollView>

Pick.java:

public class Pick_Restaurant extends ActionBarActivity {
    public void onClick_Ratings (View v) {
        Intent intent = new Intent(mContext, Ratings.class);
        startActivityForResult(intent,RATINGS);
    }
}

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 59
    versionName "0.6.4"
}

...

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
    compile files('libs/mobileservices-1.1.5.jar')
}

Full Error on Log:

04-08 17:06:40.578    3508-3508/com.myapp.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myapp.debug, PID: 3508
    java.lang.IllegalStateException: Could not find a method onClick_Ratings(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'Ratings_Button'
            at android.view.View$1.onClick(View.java:4234)
            at android.view.View.performClick(View.java:5191)
            at android.view.View$PerformClick.run(View.java:20916)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5974)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     Caused by: java.lang.NoSuchMethodException: onClick_Ratings [class android.view.View]
            at java.lang.Class.getMethod(Class.java:665)
            at android.view.View$1.onClick(View.java:4227)
            at android.view.View.performClick(View.java:5191)
            at android.view.View$PerformClick.run(View.java:20916)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5974)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

Solution

  • It looks like this is a new issue with Android 5.0.

    From this anwer, removing the Theme from the layout xml fixed this issue for them.

    So in your case, remove the theme from your layout:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
        <!--android:theme="@style/AppTheme"--> <!-- Remove this -->
        <!--................-->
      </ScrollView>
    

    And add the theme in the AndroidManifest.xml instead:

    android:theme="@android:style/AppTheme"