I am creating a custom theme for button and using android:onClick
event of Button from xml itself to handle the click of Button.
Due to some reason its crashing with below exception
java.lang.IllegalStateException: Could not find a method MyOnClick(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'button1'
And its working fine if I just remove the theme attribute from the Button, below is my theme for Button
<style name="ButtonTheme" parent="@android:style/Widget.Button">
<item name="android:textColor">#FF0000</item>
<item name="android:shadowColor">#FF000000</item>
</style>
And my Button defined in xml as below,
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_margin="20dp"
android:onClick="MyOnClick"
android:theme="@style/ButtonTheme"
android:text="Button" />
Here is my java code as well,
public void MyOnClick(View view) {
switch (view.getId()) {
case R.id.button1:
getWindow().setStatusBarColor(getResources()
.getColor(R.color.statusBarColor));
getWindow().setNavigationBarColor(getResources()
.getColor(R.color.statusBarColor));
break;
default:
break;
}
}
So, what could be the reason for crashing? I am able to handle the click event if I remove android:theme="@style/ButtonTheme"
attribute from Button Widget from xml.