机器人5和的onClick在XML布局 [英] android 5 and onClick in xml layout
问题描述
我已经设置安卓的onclick
在XML为ImageButton的,并把这种方法在我的活动。在下面5款Android的IT工作正常,但在Android的这5给我的错误。
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.
我的ImageButton code:
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"/>
我的方法code:
my method code:
public void photoDetailButtonMethod(View theButton)
{
//something
}
错误:
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)
通过查找错误,我可以看到它在 android.view.ContextThemeWrapper
寻找我的方法类,因此endup与 NoSuchMethodException
。
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)我已经加入工具:上下文=。PhotoViewerActivity
在我的布局根
1) i already added tools:context=".PhotoViewerActivity"
in the root of my layout.
2)的activiy延伸 ActionBarActivity
与 appCompat
主题。
2) the activiy extends ActionBarActivity
with appCompat
theme.
推荐答案
我有一个非常类似的问题,它只能发生在Android棒棒糖,虽然它工作正常的旧版本。看起来像在5.0中的错误或无证功能。
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.
请确保在您的的ImageButton
所在的布局文件有否 安卓主题
设置,即这样的事:
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"
相反,定义的AndroidManifest.xml
应用程序元素应用程序的主题:
Instead, define your application theme in AndroidManifest.xml
application element:
<application
...
android:theme="@android:style/Theme.Holo.Light"
... >
这篇关于机器人5和的onClick在XML布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!