【问题标题】:android.view.inflateexception binary xml file line #1 error inflating class android.widget.relativeLayoutandroid.view.inflateexception 二进制 xml 文件第 1 行错误膨胀类 android.widget.relativeLayout
【发布时间】:2014-01-12 19:18:00
【问题描述】:

我是 android 新手。
我刚刚创建了一个带有一个文本栏和 2 个按钮的新布局,但它不起作用,我正在发布堆栈跟踪和我的相关布局文件,对此有什么想法吗?我在那里看到了同样的问题,它说要减少可绘制的大小。但这对我的情况没有帮助,或者我现在对如何减少它没有太多帮助。 这是我的一些堆栈跟踪:

  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)

那是layout的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>

【问题讨论】:

    标签: java android xml eclipse android-layout


    【解决方案1】:

    你必须改变

    android:background="@layout/activity_main"
    

    到

    android:background="@drawable/yourImageName"
    

    【讨论】:

    • 但是当我去 drawable/activity_main 时找不到资源
    • 这里的activity_main是布局名称。但是您必须在可绘制文件夹中提供您的图像名称。查看我的更新答案
    • 哦..这是我的荣幸..欢迎来
    【解决方案2】:

    有时,当您使用实际上太大的大图像时也会弹出此错误,从而导致内存错误并使您的应用程序崩溃。如果您使用背景图片或任何大型资产,请确保它们存在并且它们不会太大,然后您将在雨中唱歌!

    【讨论】:

    • 哦..这条评论对我有很大帮助!谢谢!
    【解决方案3】:

    这一行表示布局的第一行:

    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"
    

    【讨论】:

      【解决方案4】:

      这是错的android:background="@layout/activity_main"应该是

      android:background="@color/backgroundColor" 
      

      或

      android:background="@drawable/backgroundimg"
      

      【讨论】:

      • @jazzb 是的,这是因为布局用于 Activity 显示在屏幕上。
      【解决方案5】:

      在我的情况下解决的问题是更改我的color.xml 的位置

      当我从我的其他项目中复制并粘贴到新项目中时,Android Studio 将其放置在 v21 文件夹中,这意味着我的模拟器 (v19) 无法访问资源文件 color.xml。

      我做了以下修复:

      1. 右键点击color.xml。
      2. 选择Refactor > Move。
      3. 在打开的对话框中,删除了后缀-v21。

      现在,我的“旧”模拟器能够读取文件了。

      希望对你有帮助..

      【讨论】:

      • 这为我修好了,谢谢。同样的东西,从其他项目复制。我的文件夹名称的添加与您的不同 - 我的后缀为 w820dp
      【解决方案6】:

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

      我发现 GUI 编辑器(使用 Eclipse 时 - 我不了解 Android Studio)有时会无故添加 buggy 属性:

      android:background="@layout/activity_main"
      

      我不确定编辑器为什么这样做 - 可能是错误或只是使用错误。

      但是,如果活动没有背景图像,则只需删除整个属性!

      【讨论】:

        【解决方案7】:

        如果您使用 Google Play 服务并显示地图,您应该在 AndroidManifest.xml 中添加 Meta 标签

        **<meta-data
                    android:name="com.google.android.gms.version"
                    android:value="@integer/google_play_services_version" />**
        

        【讨论】:

        • 这与这个问题无关。
        猜你喜欢
        • 2020-03-07
        • 2016-11-16
        • 1970-01-01
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 2016-10-13
        相关资源
        最近更新 更多