Android.view.InflateException: 二进制 XML 文件第一行出错,无法填充类 Android.widget.RelativeLayout。

17

我是 Android 新手。
我刚刚创建了一个包含一个文本框和两个按钮的新布局,但它没有起作用,我将堆栈跟踪和相对布局文件发布了出来,请问这是什么问题?我看到有人问过相同的问题,说要减小可绘制大小。但在我的情况下并没有帮助,可能是因为我不知道如何减小它。
以下是我的一些堆栈跟踪信息:

  D/AndroidRuntime(1192): Shutting down VM
W/dalvikvm(1192): threadid=1: thread exiting with uncaught exception (group=0xb60cc4f0)
E/AndroidRuntime(1192): FATAL EXCEPTION: main
E/AndroidRuntime(1192): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellomissworld/com.example.hellomissworld.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.RelativeLayout
E/AndroidRuntime(1192):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-24 18:58:31.451: E/AndroidRuntime(1192):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-24 18:58:31.451: E/AndroidRuntime(1192):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-24 18:58:31.451: E/AndroidRuntime(1192):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

这就是布局的 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@layout/activity_main"
    android:focusable="false"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentTop="true"
        android:text="@string/typehere"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:text="@string/ok" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_toLeftOf="@+id/button2"
        android:text="@string/cancel" />

</RelativeLayout>
7个回答

15

你必须进行更改

android:background="@layout/activity_main"

android:background="@drawable/yourImageName"

但是当我进入drawable/activity_main时,它找不到资源。 - jazz b
这里的activity_main是布局名称。但是你必须在其中放置你的图片名称,该名称位于drawable文件夹中。请查看我的更新答案。 - Piyush
哦,太荣幸了。欢迎。 - Piyush

13
有时候,当您使用一张过大的图片时,这个错误也会弹出来,因为它实际上太大了,导致内存错误并使您的应用程序崩溃。如果您正在使用背景图片或任何大型资源,请确保它们存在且它们不会太大,然后您将会在雨中歌唱!

9
这行文字表示布局的第一行:
android.view.InflateException: Binary XML file line #1: Error inflating class 

在第一行有一个相对布局的声明:

所以错误出现在android:background上,这不是设置布局背景的正确方式:

android:background="@layout/activity_main"

这应该是一个颜色或可绘制图像

android:background="@color/backgroundColor"android:background="#012345"

或者

android:background="@drawable/backgroundimg"

3

这是错误的语法:android:background="@layout/activity_main" 应该写成

android:background="@color/backgroundColor" 

或者
android:background="@drawable/backgroundimg"

@jazzb 是的,这就是为什么布局被用于Activity在屏幕上显示的原因。 - Piyush

3
在我的情况下,解决方法是更改我的color.xml文件的位置。
当我将它从另一个项目复制并粘贴到新项目中时,Android Studio将其放在v21文件夹中,这意味着我的仿真器(v19)无法访问资源文件color.xml
我采取了以下措施来解决这个问题:
  1. 右键单击 color.xml
  2. 选择重构 > 移动
  3. 在打开的对话框中,删除后缀-v21
现在,我的“旧”仿真器能够读取该文件。
希望这有所帮助。

这对我有帮助,谢谢。同样的事情,从其他项目复制过来的。我的文件夹名称后缀不同于你的 - 我的是带有w820dp后缀的。 - CHarris

0

我遇到了同样的问题 - 但是在一个没有背景图片的活动中。

我发现GUI编辑器(当使用Eclipse时 - 我不知道Android Studio)有时会无缘无故地添加错误的属性:

android:background="@layout/activity_main"

我不确定编辑器为什么会这样做 - 可能是一个错误或者只是使用上的问题。

然而,如果活动没有背景图片,整个属性就可以被删除!


-1
如果您正在使用Google Play服务并显示地图,则应在AndroidManifest.xml中添加元标记。
**<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />**

2
这与此问题无关。 - Jared Burrows

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接