android 5 and onClick in xml layout

2020-02-06 06:43发布

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 give 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.

7条回答
霸刀☆藐视天下
2楼-- · 2020-02-06 07:01

finally i found the problem.

all i need is to associate the xml layout to an activity. for this in the design tab of xml layout make sure you selected the right activity.

enter image description here

查看更多
The star\"
3楼-- · 2020-02-06 07:07

I was doing this with a "menuItem", and had this:

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

Which is wrong for menuItems. Then you must have this:

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

It was throwing the same error as mentioned by the OP.

Hope this helps someone...

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-06 07:12

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.

查看更多
Luminary・发光体
5楼-- · 2020-02-06 07:16

Be sure that your onClick method

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

is written outside onCreate() method and in your Activity class.

查看更多
时光不老,我们不散
6楼-- · 2020-02-06 07:17

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"
         ...  >
查看更多
做个烂人
7楼-- · 2020-02-06 07:19

Its just google play services issue and nothing to do with your coding or settings blah blah...

just update your google play services of phone and add dependencies in build.gradle file like:

compile 'com.google.android.gms:play-services:7.5.0'

查看更多
登录 后发表回答