使用可绘制 XML 时出现 IllegalStateException

发布于 2024-11-15 21:23:11 字数 4106 浏览 3 评论 0原文

当将 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

但是,当我将可绘制更改为其中一个图像(即 bruin_s1)时,它将正常工作。怎么了?

When using an ImageButton with this XML:

and the following drawable 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>

It will crash when I click that button with this exception:

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

However, when I change the drawable to one of the images (i.e. bruin_s1) it will work just fine. What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木緿 2024-11-22 21:23:11

当您将 state_pressedstate_focused 都设置为 true 时,有可能。在这种情况下,您的选择器将选择显示哪张图片?

尝试以下操作

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

谢谢
迪帕克

There is possibility when you wil get both state_pressed and state_focused as true. In that case which image your selector will select to show?

Try with the following

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

Thanks
Deepak

你げ笑在眉眼 2024-11-22 21:23:11

Caused by: java.lang.NoSuchMethodException: Drawing.onColorClick

在您的布局中,您有一个带有 android:onClick='onColorClick' 的视图,但该方法没有存在。

Caused by: java.lang.NoSuchMethodException: Drawing.onColorClick

In your layout, you have a View with an android:onClick='onColorClick', but that method doesn't exist.

呆萌少年 2024-11-22 21:23:11

错误发生在我的代码中的其他地方。在 onclick 处理程序中,我获取了按下按钮的背景,以获取某个像素的颜色,但是,当使用可绘制 xml 时,这显然会出错。

The error was somewhere else in my code. In the onclick handler, I was getting the background of the pressed button, to get the color of a certain pixel, however, when using drawable xml's, this goes wrong apparently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文