将ImageButton与此XML一起使用时:
以及以下可绘制的XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bruin_s1" android:state_pressed="false" />
<item android:drawable="@drawable/bruin_s2" android:state_pressed="true" />
<item android:drawable="@drawable/bruin_s1" android:state_focused="false" />
<item android:drawable="@drawable/bruin_s2" android:state_focused="true" />
</selector>
当我点击该按钮时会出现以下异常:
06-17 10:18:23.028: ERROR/AndroidRuntime(290): Uncaught handler: thread main exiting due to uncaught exception
06-17 10:18:23.048: ERROR/AndroidRuntime(290): java.lang.IllegalStateException: Could not execute method of the activity
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.View$1.onClick(View.java:2031)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.View.performClick(View.java:2364)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.View.onTouchEvent(View.java:4179)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.View.dispatchTouchEvent(View.java:3709)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.os.Looper.loop(Looper.java:123)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at java.lang.reflect.Method.invoke(Method.java:521)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at dalvik.system.NativeStart.main(Native Method)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): Caused by: java.lang.reflect.InvocationTargetException
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at nl.ivaldi.borre.Drawing.onColorClick(Drawing.java:135)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at java.lang.reflect.Method.invoke(Method.java:521)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): at android.view.View$1.onClick(View.java:2026)
06-17 10:18:23.048: ERROR/AndroidRuntime(290): ... 20 more
06-17 10:18:23.048: ERROR/AndroidRuntime(290): Caused by: java.lang.ClassCastException: android.graphics.drawable.StateListDrawable
06-17 10:18:23.048: ERROR/AndroidRuntime(290): ... 24 more
但是,当我将drawable更改为其中一个图像(即bruin_s1)时,它会正常工作。怎么了?
答案 0 :(得分:1)
有可能会让state_pressed
和state_focused
都为真。在这种情况下,您的选择器将选择显示哪个图像?
尝试使用以下
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/focused" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/focusedpressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pressed" />
<item android:drawable="@drawable/defaultbutton" />
</selector>
由于 迪帕克
答案 1 :(得分:1)
Caused by: java.lang.NoSuchMethodException: Drawing.onColorClick
在您的布局中,您有一个android:onClick='onColorClick'
的视图,但该方法不存在。
答案 2 :(得分:0)
错误在我的代码中的其他地方。在onclick处理程序中,我得到了按下按钮的背景,以获得某个像素的颜色,但是,当使用可绘制的xml时,这显然是错误的。