android - 异常设置 Android 2.3.4 上的 Imagebutton View

标签 android

我一直在 Android 4.0 上运行我的应用程序,它工作得很好。但是,我在运行 Android 2.3.4 的手机上对其进行了测试,应用程序崩溃了。我在尝试设置 Imagebutton View 时抛出异常:

03-07 17:21:58.789: E/AndroidRuntime(5741): FATAL EXCEPTION: main
03-07 17:21:58.789: E/AndroidRuntime(5741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.motesclient/com.example.motesclient.ConnectActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class android.widget.ImageButton
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.os.Looper.loop(Looper.java:130)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.ActivityThread.main(ActivityThread.java:3687)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at java.lang.reflect.Method.invoke(Method.java:507)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at dalvik.system.NativeStart.main(Native Method)
03-07 17:21:58.789: E/AndroidRuntime(5741): Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class android.widget.ImageButton
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.Activity.setContentView(Activity.java:1657)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at com.example.motesclient.ConnectActivity.onCreate(ConnectActivity.java:27)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-07 17:21:58.789: E/AndroidRuntime(5741):     ... 11 more
03-07 17:21:58.789: E/AndroidRuntime(5741): Caused by: java.lang.reflect.InvocationTargetException
03-07 17:21:58.789: E/AndroidRuntime(5741):     at java.lang.reflect.Constructor.constructNative(Native Method)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
03-07 17:21:58.789: E/AndroidRuntime(5741):     ... 22 more
03-07 17:21:58.789: E/AndroidRuntime(5741): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x101030e a=-1}
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.content.res.Resources.loadDrawable(Resources.java:1681)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.view.View.<init>(View.java:1961)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.widget.ImageView.<init>(ImageView.java:112)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.widget.ImageButton.<init>(ImageButton.java:85)
03-07 17:21:58.789: E/AndroidRuntime(5741):     at android.widget.ImageButton.<init>(ImageButton.java:81)
03-07 17:21:58.789: E/AndroidRuntime(5741):     ... 25 more

这是图像按钮:

<ImageButton
     android:id="@+id/connect_button"
     android:background="?android:attr/selectableItemBackground"
     android:contentDescription="@string/connect_picture"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:src="@drawable/connectxml"/>

这里是connectxml.xml文件,放在drawable文件夹中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"
            android:drawable="@drawable/connectbuttonpressed" /> <!-- pressed -->
        <item android:state_focused="true"
         android:drawable="@drawable/connectbuttonpressed" /> <!-- focused -->
        <item android:drawable="@drawable/connectbutton" /> <!-- default -->
</selector>

关于如何修复它有任何想法吗?

最佳答案

您使用的东西在 api 9 (2.3) 中不存在:

android:background="?android:attr/selectableItemBackground"

您可以转到您的 android 平台文件夹并查看给定版本支持的内容。它位于:

android-sdk\platforms\android-9\data\res\values\attr.xml

重新编辑

一个可能的解决方案是删除出现问题的行,而是添加:

style="@style/Custom.ImageButton"

然后将 styles.xml 添加到 res/values 中,如下所示:

<resources>
   <style name="Custom.ImageButton" parent="@android:style/Widget.ImageButton">
       <item name="android:background">@drawable/your_own_graphic</item>
   </style>
</resources>

还有一个 styles.xmlres/values-14 看起来像这样:

   <style name="Custom.ImageButton" parent="@android:style/Widget.ImageButton">
       <item name="android:background">?android:attr/selectableItemBackground</item>
   </style>

对于之前的错误建议,我深表歉意。

关于android - 异常设置 Android 2.3.4 上的 Imagebutton View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15276706/

相关文章:

java - 从 Android 到服务器、PC 和 Android 设备的实时视频流

android - 用于 LVL 和 fragment 兼容性支持的 Proguard Android 应用程序

android - 启动画面仅在应用程序首次启动时打开

android - java.lang.NumberFormatException : For input string: "-0.‎086167157" 异常

android - 在不耗尽电池的情况下与服务器保持持续的互联网连接

android - ServerSocket 是实现 android 推送通知的好主意吗?

java - Eclipse 中的按钮按下事件 (Android)

android - ExpandableListView 中按钮的 onClick 方法

android - linux shell - 无法将字符串与 : adb shell getprop ro. product.brand 进行比较

android - 如何更新我在 Android 上运行的使用 Unity 制作的游戏?