android膨胀异常

时间:2012-10-05 14:57:19

标签: android android-layout inflate-exception

我想使用继承为我的Android应用程序创建一个布局,即每个屏幕都有相同的背景和标题。

具有该背景和标题的布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/headerLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="2dp"
        android:text="@string/app_name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@string/ColorLabel" />

</LinearLayout>

</merge>

虽然特定屏幕(登录屏幕)如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/background" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/username_label"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="@string/Username"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/username_box"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="text" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/password_label"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="@string/Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/password_box"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPassword" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="fill"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:weightSum="2" >

            <Button
                android:id="@+id/login_button"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center|left"
                android:layout_marginRight="20dp"
                android:background="@drawable/background"
                android:text="@string/Login"
                android:textColor="@string/ColorLabel" />

            <Button
                android:id="@+id/register_button"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:background="@drawable/background"
                android:text="@string/Register"
                android:textColor="@string/ColorLabel" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

</merge>

当我想运行此代码时,会导致错误:

10-05 17:10:11.620: D/AndroidRuntime(25222): Shutting down VM
10-05 17:10:11.623: W/dalvikvm(25222): threadid=1: thread exiting with uncaught exception (group=0x40015578)
10-05 17:10:11.635: E/AndroidRuntime(25222): FATAL EXCEPTION: main
10-05 17:10:11.635: E/AndroidRuntime(25222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shopassistent/com.shopassistent.Login_Activity}: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.os.Looper.loop(Looper.java:123)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at java.lang.reflect.Method.invokeNative(Native Method)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at java.lang.reflect.Method.invoke(Method.java:507)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at dalvik.system.NativeStart.main(Native Method)
10-05 17:10:11.635: E/AndroidRuntime(25222): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:682)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:619)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.inflate(LayoutInflater.java:383)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.Activity.setContentView(Activity.java:1657)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at com.shopassistent.Login_Activity.onCreate(Login_Activity.java:12)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-05 17:10:11.635: E/AndroidRuntime(25222):    ... 11 more
10-05 17:10:11.635: E/AndroidRuntime(25222): Caused by: java.lang.reflect.InvocationTargetException
10-05 17:10:11.635: E/AndroidRuntime(25222):    at java.lang.reflect.Constructor.constructNative(Native Method)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.view.LayoutInflater.createView(LayoutInflater.java:505)
10-05 17:10:11.635: E/AndroidRuntime(25222):    ... 26 more
10-05 17:10:11.635: E/AndroidRuntime(25222): Caused by: android.content.res.Resources$NotFoundException: File #ffffff from drawable resource ID #0x7f040009: .xml extension required
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.content.res.Resources.loadColorStateList(Resources.java:1824)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.widget.TextView.<init>(TextView.java:677)
10-05 17:10:11.635: E/AndroidRuntime(25222):    at android.widget.TextView.<init>(TextView.java:369)
10-05 17:10:11.635: E/AndroidRuntime(25222):    ... 29 more

有人可以看到这个错误来自哪里吗?

4 个答案:

答案 0 :(得分:2)

这里

android:textColor="@string/ColorLabel"

它返回#FFFFFF作为字符串,而后者又被解释为可绘制的文件名,因此

Caused by: android.content.res.Resources$NotFoundException: File #ffffff from drawable resource ID #0x7f040009: .xml extension required

为textColor

使用drawable或color reference

答案 1 :(得分:1)

你有background.xml吗?

Caused by: android.content.res.Resources$NotFoundException: File #ffffff from drawable resource ID #0x7f040009: .xml extension required

答案 2 :(得分:1)

您的第一个xml甚至是复制粘贴错误,或者它无效:

<merge>标记没有结束。您应该在文件末尾添加</merge>(如果这不是复制粘贴错误)

编辑:第二个xml文件有同样的问题

答案 3 :(得分:0)

你必须删除“requestFocus” 从你的第二个文件中,改为使用它:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/background" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/username_label"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="@string/Username"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/username_box"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="text" >


        </EditText>

        <TextView
            android:id="@+id/password_label"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:text="@string/Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/password_box"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPassword" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="fill"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:weightSum="2" >

            <Button
                android:id="@+id/login_button"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center|left"
                android:layout_marginRight="20dp"
                android:background="@drawable/background"
                android:text="@string/Login"
                android:textColor="@string/ColorLabel" />

            <Button
                android:id="@+id/register_button"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:background="@drawable/background"
                android:text="@string/Register"
                android:textColor="@string/ColorLabel" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
</merge>