Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android 5 and onClick in xml layout

I have set android:onclick in xml for an imageButton and put that method in my activity. In android s below 5 it works fine but in android 5 it gives me Error.

My imageButton code:

<ImageButton 
     android:id="@+id/photo_detail"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/detail_icon"
     android:layout_alignParentLeft="true"
     android:layout_centerVertical="true"
     android:background="@drawable/image_background"
     android:onClick="photoDetailButtonMethod"/>

My method code:

public void photoDetailButtonMethod(View theButton)
{
  //something
}

The error:

java.lang.IllegalStateException: Could not find a method photoDetailButtonMethod(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.ImageButton with id 'photo_detail'
            at android.view.View$1.onClick(View.java:3994)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NoSuchMethodException: photoDetailButtonMethod [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:3987)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

By looking the error I can see it searching for my method in android.view.ContextThemeWrapper class so it endup with NoSuchMethodException.

I can't figure out how to solve this, any help?

1) I already added tools:context=".PhotoViewerActivity" in the root of my layout.

2) The activiy extends ActionBarActivity with appCompat theme.

like image 217
mehdok Avatar asked Dec 17 '14 17:12

mehdok


People also ask

Is onClick deprecated in android?

onClick is prompted deprecated.

How can use onClick method in android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

Which rule applies to a click handler called from the attribute in the layout?

The click handler, if called from the android:onClick attribute, must be public , return void , and define a View as its only parameter.

What is android ImageButton?

Android ImageButton is a user interface widget which is used to display a button having image and to perform exactly like button when we click on it but here, we add an image on Image button instead of text. There are different types of buttons available in android like ImageButton, ToggleButton etc.


2 Answers

I had a very similar problem, it happens only on Android Lollipop, whilst it works fine on the older versions. Looks like a bug or undocumented feature in 5.0.

Make sure that in the layout file where your ImageButton resides there is no android:theme set, i.e. nothing like this:

android:theme="@style/Base.Theme.AppCompat.Light"

Instead, define your application theme in AndroidManifest.xml application element:

<application 
         ...
         android:theme="@android:style/Theme.Holo.Light"
         ...  >
like image 113
Kirill K Avatar answered Oct 20 '22 12:10

Kirill K


i can't find what is the real problem, maybe some incompatiblity between eclipse and api 21 or something else.

for now i just set an onClickListener for that button.

like image 2
mehdok Avatar answered Oct 20 '22 11:10

mehdok