android.view.InflateException:二进制XML文件 - 原因未知

时间:2013-05-28 09:27:19

标签: android android-layout

启动活动时出现以下异常:

05-28 13:34:09.399: E/AndroidRuntime(502): FATAL EXCEPTION: main
05-28 13:34:09.399: E/AndroidRuntime(502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nykkos.personalize/com.nykkos.personalize.screens.SummaryCostScreen}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.os.Looper.loop(Looper.java:137)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-28 13:34:09.399: E/AndroidRuntime(502):  at java.lang.reflect.Method.invokeNative(Native Method)
05-28 13:34:09.399: E/AndroidRuntime(502):  at java.lang.reflect.Method.invoke(Method.java:511)
05-28 13:34:09.399: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-28 13:34:09.399: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-28 13:34:09.399: E/AndroidRuntime(502):  at dalvik.system.NativeStart.main(Native Method)
05-28 13:34:09.399: E/AndroidRuntime(502): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.createView(LayoutInflater.java:606)
05-28 13:34:09.399: E/AndroidRuntime(502):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.parseInclude(LayoutInflater.java:823)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:729)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-28 13:34:09.399: E/AndroidRuntime(502):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.Activity.setContentView(Activity.java:1835)
05-28 13:34:09.399: E/AndroidRuntime(502):  at com.nykkos.personalize.screens.SummaryCostScreen.onCreate(SummaryCostScreen.java:37)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.Activity.performCreate(Activity.java:4465)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-28 13:34:09.399: E/AndroidRuntime(502):  ... 11 more
05-28 13:34:09.399: E/AndroidRuntime(502): Caused by: java.lang.reflect.InvocationTargetException
05-28 13:34:09.399: E/AndroidRuntime(502):  at java.lang.reflect.Constructor.constructNative(Native Method)
05-28 13:34:09.399: E/AndroidRuntime(502):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.view.LayoutInflater.createView(LayoutInflater.java:586)
05-28 13:34:09.399: E/AndroidRuntime(502):  ... 26 more
05-28 13:34:09.399: E/AndroidRuntime(502): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f020000 a=-1 r=0x7f020000}
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.content.res.Resources.loadDrawable(Resources.java:1899)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.widget.ImageView.<init>(ImageView.java:119)
05-28 13:34:09.399: E/AndroidRuntime(502):  at android.widget.ImageView.<init>(ImageView.java:109)
05-28 13:34:09.399: E/AndroidRuntime(502):  ... 29 more
05-28 13:34:09.549: W/ActivityManager(77):   Force finishing activity com.nykkos.personalize/.screens.SummaryCostScreen

活动中的错误来自:

setContentView(R.layout.summarycost_layout);

xml文件summarycost_layout是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <include
    android:id="@+id/includeTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    layout="@layout/titlebar_layout" />

    <LinearLayout
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ListView
        android:id="@+id/summaryCostListview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white" >
    </ListView>
    </LinearLayout>

    <LinearLayout
    android:id="@+id/layoutTotal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingLeft="10dp" >

    <TextView
        android:id="@+id/txtTotal"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="Total Cost(0/0)"
        android:textColor="@color/blue"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/totalValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="$0"
        android:textColor="@color/blue" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible" />
    </LinearLayout>

    <LinearLayout
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/buttonCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/button_bg"
        android:text="@string/cancel"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/buttonAll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/button_bg"
        android:text="@string/select_all"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/buttonAccept"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/button_bg"
        android:text="@string/accept"
        android:textColor="@android:color/white" />
    </LinearLayout>

</LinearLayout>

并且上面的xml文件中引用的titlebar_layout是:

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imgBack"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:onClick="onBackClick"
    android:src="@drawable/arrow_back" />

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:textColor="@android:color/white" />

<ImageView
    android:id="@+id/imgForward"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:onClick="onForwardClick"
    android:src="@drawable/arrow_for" />

看来,问题来自于titlebar_layout的<ImageView

我有点迷失在哪里寻找异常的原因。我甚至不知道这个布局文件中是否真的存在这个问题。

你会如何找到这个问题的原因?

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我已经经历了过去OP所建议的确切问题,而@shoe rat在他的评论中提到了正确的答案。

我猜你的PNG drawables不在基本'drawable'文件夹中,Android可以在你正在测试的设备或模拟器上找到匹配项。你可能无意中将你的arrow_back和button_bg drawables复制到例如drawable-large和drawable-xlarge中,但是在设备或模拟器上进行测试,比如说小(320x280)。在我的示例中,匹配规则找不到小屏幕大小的匹配因此android.content.res.Resources $ NotFoundException:资源不是Drawable(颜色或路径)错误结果。

因此,对于快速测试,将您的arrow_back和button_bg drawables(以及您拥有的任何其他内容)复制到drawable文件夹,它应该编译并运行而不会出现错误,因为drawable文件夹中的drawables将始终被找到并用作后备。然后,当然要确保为每个屏幕尺寸创建适当大小的drawable以适应所有屏幕变化,并阅读“How Android Finds the Best-matching Resource”以更好地理解Android中的可绘制匹配过程。