java InflateException:二进制 XML 文件第 22 行:错误膨胀类 <unknown>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25704546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:30:45  来源:igfitidea点击:

InflateException: Binary XML file line #22: Error inflating class <unknown>

javaandroidxml

提问by Meneer Venus

When i try to run my application it crashes and i get the following in logcat:

当我尝试运行我的应用程序时,它崩溃了,我在 logcat 中得到以下信息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.StartActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class <unknown>

.....

.....

Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class <unknown>

.....

.....

Caused by: java.lang.reflect.InvocationTargetException

.....

.....

Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1

the error also includes to a line in my own code where i set the content view

该错误还包括在我自己的代码中设置内容视图的一行

this is the xml file i reference to

这是我参考的xml文件

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">



            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp">

                <TextView
                    android:id="@+id/summonerName"
                    android:layout_width="439dp"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:gravity="center_vertical"
                    android:hint="@string/prompt_email"
                    android:textSize="?android:textAppearanceLarge"
                    android:singleLine="true" />

                <Spinner
                    android:id="@+id/region"
                    android:layout_width="83dp"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toRightOf="@id/summonerName"
                    android:layout_alignParentRight="true"
                    />

            <Button
                android:id="@+id/btnSearch"
                style="?android:textAppearanceSmall"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="#777"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:text="@string/action_sign_in"
                android:textColor="#FFF"
                android:textStyle="bold" />

            </RelativeLayout>
        </LinearLayout>


<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#f3f3f4"
    android:choiceMode="singleChoice"
    android:divider="#dadadc"
    android:dividerHeight="1dp"/>

I have no clue what to do with these errors. Can anyone help me onto the right track?

我不知道如何处理这些错误。谁能帮我走上正确的轨道?

回答by Owain van Brakel

All your @dimens should be defined

你所有的@dimens 都应该被定义

for example

例如

<resources>
    <!-- Default margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

You might have added those in for example these folders

您可能已经在例如这些文件夹中添加了这些

  • values-sw600dp
  • values-sw600dp-land
  • values-v19
  • values-xxhdpi
  • 值-sw600dp
  • 值-sw600dp-土地
  • 值-v19
  • 值-xxhdpi

Or any other folder of course where they could belong
But don't forget that those also have to be in

或任何其他文件夹,当然它们可能属于
但不要忘记那些也必须在

  • values
  • 价值观

They have to be also defined there

它们也必须在那里定义

if that isn't the problem try to create a value-XXXX folder for every layout-XXXX folder you have

如果这不是问题,请尝试为您拥有的每个 layout-XXXX 文件夹创建一个 value-XXXX 文件夹

  • layout-xhdpi
  • values-xhdpi
  • 布局-xhdpi
  • 值-xhdpi

And

  • layout-large
  • values-large
  • 布局大
  • 值大

for example

例如

回答by Tom Groentjes

I had the same problem,

我有同样的问题,

problem was that I used a DrawerLayout and that my minSDK was set to below 14. changing that fixed the problem. The android support and appcompat packages for some reason allow you to use the DrawerLayout, but the DrawerLayout definitions for layouts are missing.

问题是我使用了 DrawerLayout 并且我的 minSDK 设置为低于 14。更改解决了问题。由于某种原因,android support 和 appcompat 包允许您使用 DrawerLayout,但缺少布局的 DrawerLayout 定义。

changing the minSDK to 14 fixed the issue.

将 minSDK 更改为 14 解决了该问题。

回答by Suresh Parmar

It might be cause of using high resolution drawable resources.check out drawable file resolutions.

这可能是使用高分辨率可绘制资源的原因。请查看可绘制文件分辨率。

回答by SONU SOURAV

For me, it was because the layout was using android:theme="@style/AppTheme" in parent Linear Layout. The App theme was defined as:

对我来说,这是因为布局在父线性布局中使用了 android:theme="@style/AppTheme"。App 主题定义为:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="actionBarTheme">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:textColorHint">@color/white</item>

I removed it and everything worked fine.

我删除了它,一切正常。