157

我收到了主题中显示的许多类型的错误。这些错误似乎是偶然的,我无法重现它们。从堆栈中我可以了解到,我的不同布局资源可能会发生此类错误。XML 的行也各不相同。

谁能解释为什么会发生此错误?我能做些什么来解决这个问题?

=============================================================

com.fsp.android.f generated the following exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fsp.android.f/com.life360.android.ui.tour.TourActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>

--------- Stack trace ---------
1. android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2649)
2. android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2674)
3. android.app.ActivityThread.access$2200(ActivityThread.java:131)
4. android.app.ActivityThread$H.handleMessage(ActivityThread.java:1975)
5. android.os.Handler.dispatchMessage(Handler.java:99)
6. android.os.Looper.loop(Looper.java:123)
7. android.app.ActivityThread.main(ActivityThread.java:4702)
8. java.lang.reflect.Method.invokeNative(Native Method)
9. java.lang.reflect.Method.invoke(Method.java:521)
10. com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11. com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12. dalvik.system.NativeStart.main(Native Method)
-------------------------------

----------- Cause -----------
android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>

1. android.view.LayoutInflater.createView(LayoutInflater.java:513)
2. com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
3. android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
4. android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
5. android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
6. android.view.LayoutInflater.inflate(LayoutInflater.java:382)
7. android.view.LayoutInflater.inflate(LayoutInflater.java:320)
8. android.view.LayoutInflater.inflate(LayoutInflater.java:276)
9. com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:208)
10. android.app.Activity.setContentView(Activity.java:1629)
11. com.solvek.sample.ui.BaseActivity.onCreate(BaseActivity.java:23)
12. com.solvek.sample.ui.tour.TourActivity.onCreate(TourActivity.java:161)
13. android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
14. android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2587)
15. android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2674)
16. android.app.ActivityThread.access$2200(ActivityThread.java:131)
17. android.app.ActivityThread$H.handleMessage(ActivityThread.java:1975)
18. android.os.Handler.dispatchMessage(Handler.java:99)
19. android.os.Looper.loop(Looper.java:123)
20. android.app.ActivityThread.main(ActivityThread.java:4702)
21. java.lang.reflect.Method.invokeNative(Native Method)
22. java.lang.reflect.Method.invoke(Method.java:521)
23. com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
24. com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
25. dalvik.system.NativeStart.main(Native Method)
-----------------------------

-------- Environment --------
Time =2010-12-20 08:27:35 AM
Device =tmobile/htc_espresso/espresso/espresso:2.1-update1/ERE27/216830:user/release-keys
Make =HTC
Model =T-Mobile myTouch 3G Slide
Product =htc_espresso
App =com.fsp.android.f, version 2.0.9 (build 1232)

这是 XML 的结果,但是在其他 xml 中会发生此类错误

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ViewFlipper android:layout_gravity="fill" android:id="@+id/flipper" android:layout_height="fill_parent" android:layout_width="fill_parent">
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_1"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_2"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_3"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_4"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_5"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_6"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_7"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_8"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_9"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_10"/>
        <ImageView android:scaleType="fitXY" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/tour_11"/>
    </ViewFlipper>

    <LinearLayout android:id="@+id/exit_bar" android:layout_gravity="top" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/blue_bar" android:visibility="invisible">
        <Button android:background="@drawable/orange_btn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:textStyle="bold" android:textColor="#000000" android:shadowColor="#ffffff" android:shadowRadius="1.6" android:shadowDx="1.5" android:shadowDy="1.3" android:id="@+id/exit_tour_btn" android:text="Exit" android:layout_marginLeft="20dip"/>
        <TextView android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_gravity="center" android:layout_width="110dip" android:layout_marginLeft="20dip" android:textSize="16dip" android:textStyle="bold" android:text="Life360 Tour"/>

    </LinearLayout>

    <Button style="@style/BlueBtn" android:text="Continue" android:layout_marginTop="40dip" android:id="@+id/continue_btn" android:visibility="gone" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" />

    <FrameLayout android:id="@+id/bottom_bar" android:layout_gravity="bottom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/blue_bar" android:visibility="invisible">
        <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/left" android:layout_gravity="left|center" android:background="@drawable/tour_left" android:layout_marginLeft="10dip"/>
        <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/right" android:layout_gravity="right|center" android:background="@drawable/tour_right" android:layout_marginRight="10dip"/>               
    </FrameLayout>
</merge>
4

33 回答 33

277

膨胀异常实际上并不是问题,而是真正来自布局中另一个更深层次的问题,然后将其包裹在InflateException. 一个常见的问题是在尝试膨胀ImageView加载可绘制资源时出现内存不足异常。如果其中一个资源具有高像素分辨率,则将占用大量内存,从而导致膨胀异常。

因此,基本上要验证所有图像可绘制对象中的像素分辨率是否只是布局所需的最低要求。

于 2014-04-21T17:14:08.940 回答
29

如果您使用VectorDrawableusing 支持库并忘记使用app:srcCompat而不是android:src

确切的错误是: Binary XML file line #XX: Error inflating class ImageView

看到那个链接

于 2016-06-09T14:07:41.717 回答
24

ViewFlipper在布局膨胀期间将所有图像加载到内存中。因为我的图像很大,它需要很多内存,我用它替换ViewFlipperImageSwitcher它可以用动画改变图像,ViewFlipper但它一次只加载一张图像。

于 2011-03-01T10:31:44.607 回答
13

在仅使用一个构造函数创建自定义视图时,我遇到了同样的错误,请尝试为您的自定义视图定义所有构造函数。

   public CustomWebView(Context context) {
        super(context);
        init();
    }

    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
于 2017-02-21T01:50:12.770 回答
11

我知道这个问题已经得到解答,但我仍然在发帖,认为可能有人会遇到这种问题。

在我的情况下,问题是我正在将我的应用程序加载到手机,它引用来自 res/layout/ 文件夹的布局和来自 res/values/dimens 的 @dimens 的值,这里是它试图访问的 font_22,它在 res/values-xlarge 中定义/尺寸。

我实际上是在更新现有项目的 UI。

我遇到了这个问题,因为我使用的是 IDE Eclipse,在为布局文件夹编写 xml 时,我使用 ctrl+space 进行提示,它显示来自 values 以及 values-xlarge 文件夹的所有值,无论我正在为哪个文件夹编写。

我也知道两个文件中的值应该相同以映射到不同的屏幕。

希望这可以帮助有人遇到这种愚蠢的问题。

于 2013-04-02T08:03:02.450 回答
10

我遇到了同样的错误,发现根本原因是:

使用应用程序上下文来膨胀视图。

Inflating with Activity Context 修复了这个错误。

于 2015-09-09T04:08:26.787 回答
9

我发现了同样的错误,花了两天时间才确定错误是什么。

该错误仅仅是因为我试图使用:android:background

而不是:app:srcCompat

在 SVG 文件中。

在你的情况下,我相信就是这样

<ImageView 
    android:scaleType="fitXY" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:src="@drawable/tour_11"      <-- the error is here !
 />

我建议以这种方式使用它

将此添加到您的build.gradle应用程序

android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

<androidx.appcompat.widget.AppCompatImageView  <-- use **AppCompatImageView** not **ImageView**
        android:scaleType="fitXY" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        app:srcCompat="@drawable/tour_11"
     />

我希望这有帮助。

于 2018-02-25T20:19:11.083 回答
6

我的图片大小只有 14Kb 但问题是设计师给了我 1011px x 1819px (分辨率太高导致 InflateException)

于 2015-05-11T15:43:45.900 回答
6

我有这个错误,因为我选择了主题作为材质主题。但是当我试图在 4.4.2 上运行应用程序时,它给出了这个错误。

解决方案:选择 Theme_holo 作为主题

于 2015-11-03T18:56:24.930 回答
6

如果有人遇到类似问题,我在夸大视图时遇到了这样的问题:

View.inflate(getApplicationContext(), R.layout.my_layout, null)

通过替换getApplicationContext()固定this

于 2018-03-31T15:05:16.713 回答
5

对我来说,日志猫中的错误消息实际上是不够的,所以我做了以下事情来找出导致问题的原因:

(在 log cat 错误消息中,它表示在我的 HomeFragment.java 中膨胀特定布局时发生错误)

  1. 我在布局膨胀之前放了一个断点
  2. 我在调试模式下运行应用程序,直到它到达特定的断点
  3. 我用光标选择了行并Evaluate expression在其上运行:
    • Run > Evaluate Expression或者alt - F8
  4. 结果向我展示了有关问题根源的更多信息,在我的情况下,这是一个可绘制文件使用tools:targetApi="lollipop"(该错误仅发生在较旧的设备上)。
于 2017-10-09T12:05:14.753 回答
5

对我来说,我的问题是我是

android:background="?attr/selectableItemBackground"

对于 LinearLayout 背景,一旦我删除它,测试就通过了。

于 2019-10-25T11:29:14.737 回答
4

此链接可能会对您有所帮助。尝试检查清单是否存在问题。如果您可以让它再次发生,请发布您的整个堆栈跟踪,以便我们可以看到错误实际上是什么。

编辑:我确定您已经检查过了,但是您用于 TourActivity 布局的 XML 文件的第 12 行是什么?

于 2010-12-22T20:35:32.317 回答
3

我知道这是一个已回答的问题,但我看不出我面临的原因。

这是Android Studio将我的drawables放在/drawable-V24我的模拟器是API 23。所以,最终它找不到它。

解决方案是将所有可绘制对象移动到 /drawable 文件夹(无 -24)。

于 2018-09-24T16:24:37.350 回答
3

您应该将图像文件从“drawable-24”文件夹复制到“drawable”文件夹。

于 2019-05-08T08:08:16.370 回答
3
  • 我只是解决了这个问题,只需从 drawable(v24) 中删除您的图像,然后将其粘贴到 drawable 文件夹中,这对我有用。

于 2019-09-20T15:42:49.127 回答
2

我遇到了同样的错误,我解决了将drawables从文件夹drawable-mdpi移动到文件夹drawable的问题。我花了一些时间才意识到,因为在 Eclipse 中一切正常,而在 Android Studio 中我遇到了这些丑陋的运行时错误。

编辑说明: 如果您正在从 eclipse 迁移到 Android Studio,并且您的项目来自 eclipse,则可能会发生这种情况,因此请注意 Android Studio 中的情况与 eclipse 略有不同。

于 2016-06-14T07:58:51.647 回答
2

就我而言,当我使用浮动操作按钮并设置时会发生此错误android:backgroundTint="#000"。然后不要设置backgroundTint,问题就解决了。希望对你有帮助。

于 2017-07-05T09:54:10.280 回答
2

我有同样的问题,它通过在可绘制文件夹中的图像前面删除带有(24)的drawable-v24并用普通的drawable替换它们来解决。

于 2019-02-26T20:17:26.337 回答
2

通过将我的所有可绘制项目从 drawable-v24 移动到 drawable 来解决它

于 2019-11-04T15:25:42.507 回答
1

我刚才遇到了这个问题,并设法弄清楚它是什么。在我的值中引用了导致问题的颜色。所以手动定义它而不是使用下拉建议中的一个。然后它起作用了!

于 2017-05-20T19:08:36.717 回答
1

当我在 API lvl 16(26 很好)上运行我的应用程序时,我遇到了同样的错误。这是因为我?attrr/colorAccent在我的形状中使用了drawable:

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

    <corners android:radius="8dp" />

    <solid android:color="@attr/colorAccent" />

</shape>

用常规颜色替换它解决了我的问题。

于 2018-11-06T07:40:38.210 回答
1

就我而言,我必须指定我在布局文件中使用的库 UI 组件的完整包名称。

于 2018-12-03T11:18:37.290 回答
1

我知道这个帖子很旧,但仍然回答它,以便其他人不应该度过不眠之夜。

我正在重构一个旧项目,其布局文件都包含硬编码的属性,例如android:maxLength = 500. 所以我决定在我的 res/dimen文件中将它注册为<dimen name="max_length">500</dimen>.

用我的 res-value 完成了近 30 个布局文件的重构。你猜怎么了?下次我运行我的项目时,它开始抛出相同的InflateException.

作为一种解决方案,需要重做我的所有更改并保持所有这些值与以前相同。

TLDR;

第1步:一切运行良好。

第 2 步:为了加强我的维护,我android:maxLength = 500<dimen name="max_length">500</dimen>and替换了android:maxLength = @dimen/max_length,这就是一切都出错的地方(用 崩溃InflateException)。

第 3 步:所有运行不良

android:maxLength = @dimen/max_length第 4 步:通过再次替换为android:maxLength = 500.Everything来重新完成我的所有工作。一切都已修复。

第5步:一切运行良好。

于 2019-08-05T13:44:12.020 回答
0

我也遇到了类似的问题。虽然对于任何人来说,这可能是代码中各种不良用法的问题,例如

  1. XML 中的错误布局标记
  2. 将大量资源直接加载到 ImageView 中导致OOM
  3. 样式使用问题。

一个最被忽视的因素可能是在膨胀视图时使用正确的上下文。请检查您没有在需要 Activity 上下文的地方使用ApplicationContext 。虽然,ApplicationContext可能不会总是以错误告终,但根据您的视图层次结构,这可能是至关重要的。

我用 BAD 上下文解决了我的问题(在 4 天内使用ApplicationContext而不是Activity!所以尝试一下,如果解决了。快乐编码!

于 2018-05-04T10:48:08.540 回答
0

我遇到了同样的问题,我在 xml 中使用了视图标签而不是视图。替换为 View 类解决了这个问题。

希望对你也有帮助。。

于 2018-07-23T06:55:40.767 回答
0

我们需要检查 API 版本。我曾经给我LinearLayout喜欢的背景颜色

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

当然我有同样的错误,as_royalblue.xmldrawable文件夹内

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:endColor="@color/royalblue_s"
        android:startColor="@color/royalblue_e" />
</shape>

以及我是如何修复它的,实际上它似乎是 Api 问题,所以我们需要检查 api 级别是否高于 API 24,以便我们能够使用我们喜欢的方式。但如果是24以下我们需要避免使用,突出一个正常的颜色或一种颜色而不是颜色渐变混合一种。

fun checkAPI_N(): Boolean {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
            return true
        else
            return false
    }

如果可以,请为您的线性布局提供 id 并设置背景

 if(UtilKotlin.checkAPI_N()){
            linlay_act_menu_container.setBackgroundResource(R.drawable.a_6)
            linlay_act_menu_logo.setBackgroundResource(R.drawable.as_strain)
        }else {//todo normal color background setting}
于 2018-08-24T14:12:55.123 回答
0

对我来说,这是因为我试图在同一个布局文件中将 xml 矢量图形图像与普通 png 混合,

只有在我用 pngs 替换了 xml 矢量图形之后它才开始工作

于 2019-03-04T08:29:26.920 回答
0

我从一个奇怪的设备中遇到了同样的错误Crashlytics:Motorola One Vision Android 5.1 and 20 GB (free) RAM, rooted 这个设备最初带有 Android 9.0, 4 GB RAM

可能有人试图破解我目前正在开发的应用程序并且在处理多 APK 时遇到了问题。因此无法找到可绘制对象并且应用程序会导致崩溃

于 2020-04-30T08:30:44.740 回答
0

我在 Kitkat 上遇到了同样的错误,这是因为我在其中一个 imageViews 上有 android:tint。它在 Lollipop 上可以正常工作,但在 Kikkat 上会因 Error Inflating class 崩溃。

通过在我正在处理的 AppCompatImageView 上使用 app:tint 来修复它。

于 2020-09-01T15:15:01.940 回答
0

我通过更新我的应用程序主题以从材料组件继承来解决这个问题。

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>
于 2020-09-27T11:09:02.650 回答
0

只需移动图像或形状

  • drawable-v24 到可绘制文件夹

    在此处输入图像描述

膨胀异常:

android.view.InflateException: Binary XML file line #32: Error inflating class androidx.appcompat.widget.SearchView
    at android.view.LayoutInflater.createView(LayoutInflater.java:633)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
    at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)
    at com.foamkart.Fragment.SearchFragment.onCreateView(SearchFragment.kt:37)
于 2021-01-05T05:41:09.950 回答
0

如果您正在使用导航组件并尝试充气androidx.fragment.app.FragmentContainerView,请确保您在导航图startDestination上设置了一个!

于 2021-12-27T03:30:49.267 回答