我一直在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:
<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>
有关如何修复的想法吗?
答案 0 :(得分: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>
我很抱歉先前的建议是错误的。
答案 1 :(得分:2)
一切看起来都对我好。我会尝试清理项目。
假设您正在使用Eclipse:Project - &gt;清洁 - &gt;全部清理
有时eclipse无法使用最新的xml更改来更新R.java。