Specifying theme and onClick in XML causes NoSuchMethodException attributing it to ContextThemeWrapper

111 Views Asked by At

Edit: Answer provided below is to convert the usage of android:theme to simply style, so android:theme="@style/foo" would become style="@style/foo"

Original Post:

I'm in the process of trying to clean up our layout files and as such I'm taking out some things that would be considered "style" elements and creating appropriate blocks and then within the layout, the individual components use android:theme to reference the new style. An example would be: Before:

<TextView
    android:id="@+id/my_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:textStyle="bold"
    android:onClick="onMyTextClick"
/>

After:

<TextView
    android:id="@+id/my_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onMyTextClick"
    android:theme="@style/text_style"
/>

And the styles.xml file

<style name="text_style">
    <item name="android:textSize">@dimen/font_small</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/text_content_light</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:padding">@dimen/padding_light</item>
</style>

Running this and then clicking on the new TextView will generate the NoSuchMethodException and a sample stacktrace is below. If I remove the android:theme line, it works just fine....

06-17 17:12:44.306 E/AndroidRuntime( 3057): java.lang.IllegalStateException: Could not find a method onMyTextClick(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class TextView with id 'my_text_view'
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.view.View$1.onClick(View.java:4215)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.view.View.performClick(View.java:5162)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.view.View$PerformClick.run(View.java:20873)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.os.Handler.handleCallback(Handler.java:739)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.os.Looper.loop(Looper.java:145)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.app.ActivityThread.main(ActivityThread.java:5837)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at java.lang.reflect.Method.invoke(Native Method)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at java.lang.reflect.Method.invoke(Method.java:372)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
06-17 17:12:44.306 E/AndroidRuntime( 3057): Caused by: java.lang.NoSuchMethodException: onMyTextClick [class android.view.View]
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at java.lang.Class.getMethod(Class.java:665)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     at android.view.View$1.onClick(View.java:4208)
06-17 17:12:44.306 E/AndroidRuntime( 3057):     ... 10 more
0

There are 0 best solutions below