android.view.InflateException:二进制 XML 文件第 12 行:错误膨胀类<未知>;
我收到主题中显示的许多此类错误。这些错误似乎是偶然的,我无法重现它们。从堆栈中我可以了解到,我的不同布局资源可能会发生此类错误。 XML 的行也各不相同。
谁能解释为什么会出现这个错误?我能做些什么来解决这个问题?
Stack
=============================================================
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
Inflate 异常实际上并不是问题,而是来自布局中的另一个更深层次的问题,然后将其包装在
InflateException
中。一个常见问题是尝试膨胀加载可绘制资源的
ImageView
时出现内存不足异常。如果这些资源之一具有高像素分辨率,则将占用大量内存,从而导致膨胀异常。因此,基本上要验证所有图像可绘制对象中的像素分辨率是否只是布局所需的最低分辨率。
The inflate exception is not actually the problem but really comes from another deeper issue in your layout that is then wrapped in an
InflateException
.A common issue is an out of memory exception when trying to inflate an
ImageView
loading a drawable resource. If one of these resources has a high pixel resolution it would take a lot of memory causing then an inflate exception.So basically verify that the pixel resolution in all your image drawables is just the minimum necessary for your layout.
如果您使用支持库的
VectorDrawable
并且忘记使用app:srcCompat
而不是android:src
,也可能会发生这种情况确切的错误是:
二进制 XML 文件行 #XX:膨胀类 ImageView 时出错
查看该链接
This can also happen if you use a
VectorDrawable
using support library and forgot to useapp:srcCompat
instead ofandroid:src
Exact error is:
Binary XML file line #XX: Error inflating class ImageView
See that link
ViewFlipper
在布局膨胀期间将所有图像加载到内存中。因为我的图像很大,需要很多内存,所以我用ImageSwitcher
替换了ViewFlipper
,它可以像ViewFlipper
一样用动画更改图像,但它只加载一个当时的图像。ViewFlipper
loads all images into memory during layout inflating. Because my images are big it takes many memory, I replacedViewFlipper
withImageSwitcher
which can change the images with animation likeViewFlipper
but it loads only one image at the time.仅使用一个构造函数创建自定义视图时,我遇到了同样的错误,请尝试为自定义视图定义所有构造函数。
I had the same error when creating custom view with only one constructor, try to define all constructor for your custom views.
我遇到了同样的错误,发现根本原因是:
使用应用程序上下文来膨胀视图。
使用 Activity Context 进行膨胀修复了该错误。
I encountered the same bug and found the root reason is:
Use Application Context to inflate view.
Inflating with Activity Context fixed the bug.
我知道这个问题已经得到解答,但我仍然抱着可能有人遇到此类问题的想法发帖。
就我而言,问题是我正在将应用程序加载到手机,该应用程序引用 res/layout/ 文件夹中的布局和 res/values/dimens 中 @dimens 的值,这里它是尝试访问的 font_22,它在 res/values-xlarge 中定义/尺寸。
我实际上正在更新现有项目的用户界面。
我遇到了这个问题,因为我使用的是 IDE Eclipse,在为布局文件夹编写 xml 时按 ctrl+space 进行提示,它会显示 value 以及 value-xlarge 文件夹中的所有值,无论我正在编写哪个文件夹。
我还知道两个文件中的值应该相同才能映射到不同的屏幕。
希望这可以帮助有人遇到这种愚蠢的问题。
I know the question is already answered but still I'm posting with thought that may someone run into this kind of problem.
In my case problem is i'm loading my application to phone which refer layouts from res/layout/ folder and values for @dimens from res/values/dimens here it's font_22 which it's trying to access and it's define in res/values-xlarge/dimens.
I'm actually updating UI of existing project.
I ran into this problem because I'm using IDE Eclipse where I ctrl+space for hint while writing xml for layout folder it displays all values from values as well as values-xlarge folder regardless of for which folder I'm writing.
I also know that the values in both files should be same to mapped for different screens.
Hope this may help someone run into this kind of silly problem.
我发现了同样的错误,花了两天时间才确定错误是什么。
该错误只是因为我试图
使用:
android:background
而不是:app:srcCompat
在 SVG 文件中
。就您而言,我相信这就是
我建议使用它的方式,
将其添加到您的 build.gradle 应用程序
中,
我希望这会有所帮助。
I found the same error, and it took two days to identify what the error was.
The error was simply because I was trying to use:
android:background
Rather than:
app:srcCompat
in an SVG file.
In your case, I believe this is it
I recommend using it this way
add this to your build.gradle app
and
I hope this helps.
我遇到此错误是因为我选择主题作为材质主题。但当我尝试在 4.4.2 上运行应用程序时,它出现了此错误。
解决方案:选择 Theme_holo 作为主题
I had this error because i selected theme as Material theme. But as i was trying to run app on 4.4.2 it gave this error.
Solution : Select Theme_holo as theme
我的图片大小只有14Kb,但问题是设计师给了我1011px x 1819px(分辨率太高因此导致InflateException)
My picture size is only 14Kb but the problem is that the designer gave me 1011px x 1819px (resolution too high hence causing the InflateException)
如果有人遇到类似的问题,我在膨胀视图时遇到了这样的问题:
View.inflate(getApplicationContext(), R.layout.my_layout, null)
通过替换 getApplicationContext() 修复/code> 与
this
In case someone gets similar issues, I had such an issue while inflating the view:
View.inflate(getApplicationContext(), R.layout.my_layout, null)
fixed by replacing
getApplicationContext()
withthis
对我来说,日志猫中的错误消息实际上是不够的,所以我做了以下操作来找出导致问题的原因:(
在日志猫错误消息中,它说在我的 HomeFragment.java 中膨胀特定布局时发生错误)
Evaluate expression
:运行>评估表达式
或alt - F8
tools:targetApi="lollipop" 的可绘制文件
(该错误仅发生在较旧的设备上)。For me, the error message was actually insufficient in the log cat, so here's what I did to figure out what caused the problem:
(In the log cat error message, it said the error occurred while inflating a specific layout in my HomeFragment.java)
Evaluate expression
on it:Run > Evaluate Expression
oralt - F8
tools:targetApi="lollipop"
(the bug only occurred on older devices).对我来说,我的问题是我是
一个 LinearLayout 背景,一旦我删除它,测试就通过了。
For me, my issue was that I was
To a LinearLayout background, once I removed it the test passed.
此链接可能会对您有所帮助。尝试检查您的清单是否存在问题。如果您可以让它再次发生,请发布整个堆栈跟踪,以便我们可以看到错误到底是什么。
编辑:我确信您已经检查过这一点,但是您用于 TourActivity 布局的 XML 文件的第 12 行是什么?
This link may help you. Try checking in your manifest for problems. If you can get it to happen again, post your entire stack trace so that we can see what the error actually is.
EDIT: I'm sure you've checked this, but what is on line 12 of the XML file you use for the TourActivity layout?
我知道这是一个已回答的问题,但我看不到我面临的原因。
Android Studio 将我的可绘制对象放置在 /drawable-V24 中
我的模拟器是API 23。所以,最终它找不到它。
解决方案是将所有可绘制对象移动到 /drawable 文件夹(否 -24)。
I know it is an answered question, but I do not see the cause that I faced.
It was that Android Studio placed my drawables in /drawable-V24
My emulator is API 23. So, eventually it can not find it.
The solution was to move all drawables to /drawable folder (no -24).
您应该将图像文件从“drawable-24”文件夹复制到“drawable”文件夹。
You should copy the image files from the "drawable-24" folder to the "drawable" folder.
我只是解决这个问题,只需从drawable(v24)中删除您的图像,然后将其粘贴到对我有用的drawable文件夹中。
I just solve this issue just delete your images from drawable(v24) and just paste it in drawable folder this works for me.
我遇到了同样的错误,我解决了将可绘制对象从文件夹可绘制-mdpi 移动到文件夹可绘制的问题。我花了一些时间才意识到,因为在 Eclipse 中一切都运行良好,而在 Android Studio 中我却遇到了这些丑陋的运行时错误。
编辑注释:
如果您要从 Eclipse 迁移到 Android Studio,并且您的项目来自 Eclipse,则可能会发生这种情况,因此请注意,Android Studio 中的情况与 Eclipse 略有不同。
I had the same error and I solved moving my drawables from the folder drawable-mdpi to the folder drawable. Took me some time to realize because in Eclipse everything worked perfectly while in Android Studio I got these ugly runtime errors.
Edit note:
If you are migrating from eclipse to Android Studio and your project is coming from eclipse it may happen, so be careful that in Android Studio things a little differs from eclipse.
我刚才遇到了这个问题,并设法弄清楚它是什么。在我的值中引用了导致问题的颜色。因此,手动定义它,而不是使用下拉建议中的一个。然后它就起作用了!
I had this problem just now and managed to figure out what it was. Was referencing a colour in my values that was causing problems. So defined it manually instead of using one from the dropdown suggestions.Then it worked!
就我而言,当我使用浮动操作按钮并设置
android:backgroundTint="#000"
时,会发生此错误。然后只要不设置backgroundTint
问题就解决了。希望对您有帮助。
In my case,this error happen when I use the Floating action button and set
android:backgroundTint="#000"
. Then just don't setbackgroundTint
and problem solved.Hope it's helpful for you.
我遇到了同样的问题,它通过删除可绘制文件夹中图像前面的 (24) 的可绘制-v24 并将其替换为普通可绘制来解决。
i had the same issue and it solved by removing drawable-v24 with (24) in front of images in drawable folder and replacing them with ordinary drawables.
通过将所有可绘制项目从可绘制-v24 移动到可绘制来解决此问题
Solved it by moving all my drawable items from drawable-v24 to drawable
当我在 API lvl 16 上运行我的应用程序时,我遇到了同样的错误(26 很好)。这是因为我在我的可绘制形状中使用了
?attrr/colorAccent
:用常规颜色替换它解决了我的问题。
I had same error when I ran my app on API lvl 16 (26 was fine). It was because I used
?attrr/colorAccent
in my shape drawable:Replacing that with a regular color fixed my problem.
就我而言,我必须指定在布局文件中使用的库 UI 组件的完整包名称。
In my case I've to specify the complete package name of library UI component that I use in my layout file.
我知道这个帖子已经很老了,但仍然回答它,以免其他人度过不眠之夜。
我正在重构一个旧项目,其布局文件都包含硬编码
属性,例如
android:maxLength = 500
。所以我决定将其注册到我的res/dimen
文件为500
。使用我的 res-value 完成了近 30 个布局文件的重构。你猜怎么着?下次我运行我的项目时,它开始抛出相同的
InflateException
。作为解决方案,需要重做所有更改并保持所有这些值与以前相同。
TLDR;
第 1 步:一切运行良好。
第 2 步:为了加强维护,我将
android:maxLength = 500
替换为500
和android: maxLength = @dimen/max_length
,这就是一切出错的地方(因InflateException
崩溃)。第 3 步:全部运行不良
第 4 步:再次用
android:maxLength = 500
替换android:maxLength = @dimen/max_length
重新完成所有工作。一切都已修复。第5步:一切运行良好。
I know this thread is old, but still answering it so that no-one else should spend sleepless nights.
I was refactoring an old project, whose layout files all contained hardcoded
attributes such as
android:maxLength = 500
. So I decided to register it in myres/dimen
file as<dimen name="max_length">500</dimen>
.Finished refactoring almost 30 layout files with my res-value. Guess what? the next time I ran my project it started throwing the same
InflateException
.As a solution, needed to redo my all changes and keep all-those values as same as before.
TLDR;
step 1: All running good.
step 2: To boost my maintenance I replaced
android:maxLength = 500
with<dimen name="max_length">500</dimen>
andandroid:maxLength = @dimen/max_length
, that's where it all went wrong(crashing withInflateException
).step 3: All running bad
step 4: Re-do all my work by again replacing
android:maxLength = @dimen/max_length
withandroid:maxLength = 500
.Everything got fixed.step 5: All running good.
只需移动图像或形状
drawable-v24到drawable文件夹
InflateException:
just move image or shape
InflateException:
我也遇到了类似的问题。虽然对于任何人来说,这可能是代码中各种不良用法的问题,例如
一个最容易被忽视的因素可能是在夸大视图时使用正确的上下文。请检查您是否没有在需要 Activity 上下文的地方使用 ApplicationContext。虽然,ApplicationContext可能不会总是出错,但根据您的视图层次结构,它可能至关重要。
我通过错误的上下文解决了我的问题(在 4 天内使用 ApplicationContext 而不是 Activity!所以尝试一下是否可以解决。快乐编码!
I also faced a similar issue. While for anyone this may be an issue of various bad usages in Code such as
One most ignored factor may be usage of correct Context while inflating views. Please check that you are not using ApplicationContext where an Activity context is required. While, ApplicationContext may not end up in error always, but depending upon your view hierarchy it may be crucial.
I resolved my issue with a BAD context (using ApplicationContext instead of Activity in a span of 4 days! So try if that solves. Happy Coding!!
我遇到了同样的问题,我在 xml 中使用了视图标签而不是视图。替换为 View 类解决了该问题。
希望它也对你有帮助..
I had faced the same issue, I had used view tag in xml instead of View. Replacing to View class fixed the issue.
Hope it helps for you too..
我们需要检查 API 版本。我曾经给我的 LinearLayout 提供背景颜色,就像
肯定我有同样的错误,在
drawable
文件夹中的
as_royalblue.xml
以及如何我修复了它,实际上这似乎是 Api 问题,所以我们需要检查 api 级别是否高于 API 24,以便我们能够使用我们喜欢的方式。但如果低于 24,我们需要避免使用,只是给出正常颜色或一种颜色而不是颜色渐变混合颜色。给你的线性布局 id 并设置背景(如果可以的话)
We need to check API Version. I used to give background color to my
LinearLayout
likefor sure I had the same error,
as_royalblue.xml
insidedrawable
folderand how I fixed it, actually it seems Api problem so we need to check the api level if it is above API 24 so we are able to use the way we like. But if it is under 24 we need to avoid usage, juts give a normal color or one color not color gradential mixed one.
give id to your linearlayouts and set backgrounds if its ok
对我来说,发生这种情况是因为我尝试在同一布局文件中将 xml 矢量图形图像与普通 png 混合,
只有在我用 png 替换 xml 矢量图形后它才开始工作
For me it happened because I tried to mix xml vector graphics images with ordinary png in the same layout file,
only after I replaced the xml vector graphics with pngs it started to work
我在一个奇怪的设备上的
Crashlytics
中遇到了同样的错误:Motorola One Vision Android 5.1 和 20 GB(免费)RAM,已 root
该设备最初配备 Android 9.0、4 GB RAM
可能有人试图破解我当前正在开发的应用程序,并且在处理多 APK 时遇到问题。因此无法找到可绘制对象并且应用程序会崩溃
I had the same error in
Crashlytics
from a strange device:Motorola One Vision Android 5.1 and 20 GB (free) RAM, rooted
This device comes originally with Android 9.0, 4 GB RAM
Probably someone is trying to hack the app I'm currently developing and has problems dealing with multi-APKs. So drawables can't be found and the app delivers the crash