2

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

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>

关于如何解决它的任何想法?

4

2 回答 2

6

您使用 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.xml看起来像res/values-14这样:

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

我很抱歉之前的建议是错误的。

于 2013-03-07T17:28:43.850 回答
2

一切对我来说都是正确的。我会尝试只清理项目。

假设您使用的是 Eclipse:项目 -> 清理 -> 全部清理

有时 Eclipse 无法使用最新的 xml 更改更新 R.java。

于 2013-03-07T16:56:40.360 回答