1

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

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

看来,问题来自<ImageViewtitlebar_layout。

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

您将如何寻找导致此问题的原因?

任何帮助表示赞赏。

4

1 回答 1

1

我过去曾遇到过 OP 建议的确切问题,@shoe rat 在他的评论中暗示了上面的正确答案。

我猜你的 PNG drawable 不在基本的“drawable”文件夹中,Android 可以在你正在测试的设备或模拟器上找到匹配项。您可能无意中将您的 arrow_back 和 button_bg 可绘制对象复制到例如 drawable-large 和 drawable-xlarge 中,但正在一个设备或模拟器上进行测试,例如小 (320x280)。在我的示例中,匹配规则找不到小屏幕尺寸的匹配项,因此 android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path) 错误结果。

因此,为了进行快速测试,请将您的 arrow_back 和 button_bg 可绘制对象(以及您拥有的任何其他可绘制对象)复制到可绘制文件夹,它应该可以无错误地编译和运行,因为可绘制文件夹中的可绘制对象将始终被找到并用作后备。然后当然要确保为每个屏幕尺寸创建适当大小的可绘制对象以适应所有屏幕变化,并阅读“ Android 如何找到最佳匹配资源”以更好地了解 Android 中的可绘制匹配过程。

于 2013-06-15T11:20:42.613 回答