android.view.InflateException:二进制XML文件行#12:错误导致类<unknown> </unknown>

时间:2010-12-22 19:35:51

标签: android xml resources android-inflate

我收到了很多在subj中显示的种类错误。这些错误似乎是偶然的,我无法重现它们。从堆栈我可以了解到我的不同布局资源可能会出现这样的错误。 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的结果,但是在其他xmls

中会出现这种错误
<?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>

32 个答案:

答案 0 :(得分:251)

膨胀异常实际上并不是问题,但实际上来自布局中的另一个更深层次的问题,然后将其包装在InflateException中。 一个常见问题是在尝试加载加载可绘制资源的imageview时出现内存不足异常。如果其中一个资源具有较高的像素分辨率,则需要大量内存才会导致膨胀异常。

因此,基本上验证drawables图像中的像素分辨率只是布局所需的最小值。

答案 1 :(得分:24)

在布局膨胀期间,

ViewFlipper将所有图像加载到内存中。由于我的图片很大,需要很多内存,我将ViewFlipper替换为ImageSwitcher,可以使用ViewFlipper等动画更改图像,但此时只加载一张图像。

答案 2 :(得分:19)

如果您使用VectorDrawable使用支持库并忘记使用app:srcCompat代替android:src

,也会发生这种情况

确切的错误是:  二进制XML文件行#XX:错误导致类ImageView

See that link

答案 3 :(得分:11)

我知道这个问题已经得到了回答,但我仍然认为可能会有人遇到这类问题。

在我的情况下,问题是我正在将我的应用程序加载到手机,它从res / layout /文件夹中引用布局,并从res / values / dimens引用@dimens的值,这是它正在尝试访问的font_22并且它在res /中定义值-XLARGE /梦诗。

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

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

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

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

答案 4 :(得分:8)

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

   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();
    }

答案 5 :(得分:5)

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

答案 6 :(得分:4)

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

使用应用程序上下文来扩充视图。

充满活动背景修复了错误。

答案 7 :(得分:4)

我遇到了这个错误,因为我选择了主题作为材质主题。但是当我试图在4.4.2上运行app时,它给出了这个错误。

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

答案 8 :(得分:4)

This link可能会对您有所帮助。尝试检查清单中的问题。如果你能再次发生这种情况,请发布整个堆栈跟踪,以便我们可以看到错误实际上是什么。

编辑:我确定你已经检查了这个,但是你在TourActivity布局中使用的XML文件的第12行是什么?

答案 9 :(得分:3)

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

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

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

答案 10 :(得分:3)

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

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

通过将getApplicationContext()替换为this

来修复

答案 11 :(得分:2)

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

答案 12 :(得分:2)

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

修改记事 如果你从eclipse迁移到Android Studio并且你的项目来自eclipse可能会发生,所以要小心,在Android Studio中,与eclipse有点不同。

答案 13 :(得分:1)

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

答案 14 :(得分:1)

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

那是Android Studio将我的可绘制对象放置在/ drawable-V24中 我的模拟器是API23。因此,最终找不到它。

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

答案 15 :(得分:1)

我在API lvl 16上运行我的应用程序时遇到了相同的错误(26很好)。这是因为我在可绘制图形中使用了?attrr/colorAccent

<?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>

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

答案 16 :(得分:1)

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

答案 17 :(得分:1)

我知道此线程很旧,但仍在回答,所以没有人可以度过不眠之夜。

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

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

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

TLDR;

第1步:一切顺利。

第2步:为了加强维护,我将android:maxLength = 500替换为<dimen name="max_length">500</dimen>android:maxLength = @dimen/max_length,这就是所有错误的地方(崩溃InflateException)。

第3步:一切运行不正常

第4步:通过再次将android:maxLength = @dimen/max_length替换为android:maxLength = 500,重新完成我的所有工作。

第5步:一切正常。

答案 18 :(得分:0)

只需移动图像或形状

  • <块引用>

    drawable-v24 到 drawable 文件夹

    enter image description here

膨胀异常:

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)

答案 19 :(得分:0)

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

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

答案 20 :(得分:0)

我在Kitkat上遇到了相同的错误,这是因为我在其中一个imageViews上安装了android:tint。在Lollipop上可以正常使用,但是在Kikkat上使用Error Inflating类崩溃。

使用我正在处理的AppCompatImageView上的app:tint修复了该问题。

答案 21 :(得分:0)

我在一个奇怪的设备上Crashlytics中遇到了相同的错误: 根植于摩托罗拉One Vision Android 5.1和20 GB(免费)RAM 该设备最初配备了Android 9.0、4 GB RAM

可能有人正在尝试破解我当前正在开发的应用程序,并且在处理多个APK时遇到问题。因此找不到可绘制对象,该应用会导致崩溃

答案 22 :(得分:0)

通过将我所有的可绘制项目从drawable-v24移至drawable来解决了该问题

答案 23 :(得分:0)

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

android:background="?attr/selectableItemBackground"

对于LinearLayout背景,一旦将其删除,则测试通过。

答案 24 :(得分:0)

  • 我只是解决了此问题,只是从drawable(v24)中删除了您的图片,然后将其粘贴到了drawable文件夹中,这对我有用。

答案 25 :(得分:0)

发生这种情况是因为我试图将xml矢量图形图像与普通png混合在同一布局文件中,

仅当我用png替换xml矢量图形后,它才开始起作用

答案 26 :(得分:0)

我遇到了同样的问题,并通过在drawable文件夹中的图像前面用(24)删除了drawable-v24并将其替换为普通的drawable来解决了此问题。

答案 27 :(得分:0)

对于我来说,我必须指定在布局文件中使用的库UI组件的完整软件包名称。

答案 28 :(得分: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.xml文件夹内的drawable上有同样的错误

<?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赋予您的linearlayout并设置背景(如果可以的话)

 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}

答案 29 :(得分:0)

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

希望它对您也有帮助。

答案 30 :(得分:0)

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

  1. XML格式错误的布局标记
  2. 将重资源直接加载到ImageView中,从而生成OOM
  3. 使用样式的问题。
  4. 一个最被忽视的因素可能是在展示视图时使用正确的上下文。请检查您是否使用 ApplicationContext ,其中需要使用Activity上下文。虽然, ApplicationContext 可能不会总是出错,但根据您的视图层次结构,这可能是至关重要的。

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

答案 31 :(得分:0)

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

错误只是因为我试图使用一个:app:srcCompat

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

在SVG文件中。

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

import numpy as np

data = {'city': np.array(['chicago','champaign','kansas city','dallas','champaign','chicago']),
        'state': np.array(['IL','IL','KA','TX','IL','IL']),
        'transaction_dt': np.array(['1-1-2018','2-1-2018','3-1-2018','7-1-2018','1-1-2018','2-1-2018'])}

res = data['transaction_dt'][data["state"] == "IL"]

我希望这会有所帮助。