我收到了很多在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>
膨胀异常实际上并不是问题,但实际上来自布局中的另一个更深层次的问题,然后将其包装在InflateException中。一个常见问题是在尝试加载加载可绘制资源的imageview时出现内存不足异常。如果其中一个资源具有较高的像素分辨率,则需要大量内存才会导致膨胀异常。
因此,基本上验证drawables图像中的像素分辨率只是布局所需的最小值。
Evaluate expression
可能会帮助你。尝试检查清单中的问题。如果你可以再次发生它,发布你的整个堆栈跟踪,以便我们可以看到错误实际是什么。
编辑:我确定你已经检查了这个,但是你在TourActivity布局中使用的XML文件的第12行是什么?
对我来说,日志cat中的错误信息实际上是不够的,所以我在这里找出导致问题的原因:
(在log cat错误消息中,它说在我的HomeFragment.java中膨胀特定布局时发生了错误)
Run > Evaluate Expression
:
alt - F8
或tools:targetApi="lollipop"
android:backgroundTint="#000"
的可绘制文件(该错误仅发生在较旧的设备上)。我知道这是一个已回答的问题,但我没有看到我面临的原因。
这是Android Studio将我的drawables放在/ drawable-V24我的模拟器是API 23.所以,最终它找不到它。
解决方案是将所有drawables移动到/ drawable文件夹(no -24)。
您应该将图像文件从“drawable-24”文件夹复制到“drawable”文件夹。
我有同样的错误,我解决了从drawable-mdpi文件夹移动我的drawables到文件夹drawable。花了我一些时间才意识到,因为在Eclipse中,一切都很完美,而在Android Studio中我遇到了这些丑陋的运行时错误。
编辑说明:如果您要从eclipse迁移到Android Studio并且您的项目来自eclipse,则可能会发生这种情况,因此请注意Android Studio中的内容与eclipse略有不同。
在我的情况下,当我使用浮动操作按钮并设置backgroundTint
时会发生此错误。然后就是不要设置?attrr/colorAccent
并解决问题。希望它对你有所帮助。
我刚才遇到了这个问题,并设法找出它是什么。引用了导致问题的值中的颜色。因此,手动定义它,而不是使用下拉建议中的一个。然后它工作!
当我在API lvl 16上运行我的应用程序时,我遇到了同样的错误(26很好)。这是因为我在我的形状中使用了<?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>
drawable:
android:maxLength = 500
用常规颜色替换它解决了我的问题。
我有同样的问题,它解决了删除drawable-v24与(24)在可绘制文件夹中的图像前面,并用普通的drawables替换它们。
我知道这个线程已经过时了,但仍在回答这个问题,以至于没有其他人应该度过不眠之夜。
我正在重构一个旧项目,其布局文件都包含硬编码属性,如res/dimen
。所以我决定在我的<dimen name="max_length">500</dimen>
文件中注册为InflateException
。
用我的res值完成了几乎30个布局文件的重构。你猜怎么着?下次我运行我的项目时,它开始抛出相同的android:maxLength = 500
。
作为一种解决方案,需要重做我的所有更改并保持所有这些值与以前一样。
TLDR;
第1步:一切运行良好。
第2步:为了提高我的维护,我用<dimen name="max_length">500</dimen>
和android:maxLength = @dimen/max_length
取代了InflateException
,这就是它出错的地方(与android:maxLength = @dimen/max_length
崩溃)。
第3步:一切都运行不好
第4步:再次用android:maxLength = 500
替换android:background
重新完成所有工作。一切都得到了修复。
第五步:一切运行良好。
ViewFlipper
在布局膨胀期间将所有图像加载到内存中。因为我的图像很大,需要很多记忆,我用ViewFlipper
替换了ImageSwitcher
,ViewFlipper
可以像VectorDrawable
那样用动画来改变图像,但是当时只加载一个图像。
我发现了同样的错误,花了两天的时间来确定错误是什么。
错误只是因为我试图使用一个: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文件中。
在你的情况下,我相信这就是它
OOM
我希望这有帮助。
我也遇到过类似的问题。对于任何人来说,这可能是代码中各种不良用法的问题,例如
LinearLayout
一个最被忽视的因素可能是在膨胀视图时使用正确的上下文。请检查您是否在使用需要Activity上下文的ApplicationContext。虽然,ApplicationContext可能不会总是出错,但根据您的视图层次结构,它可能是至关重要的。
我用BAD上下文解决了我的问题(在4天的时间内使用ApplicationContext而不是Activity!所以如果能解决的话请尝试。快乐编码!!
我遇到过同样的问题,我在xml而不是View中使用了view标签。替换为View类修复了该问题。
希望它对你有帮助..
我们需要检查API版本。我曾经给我的<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>
fun checkAPI_N(): Boolean {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
return true
else
return false
}
以及我如何修复它,实际上它似乎是Api问题所以我们需要检查api级别,如果它高于API 24,那么我们就可以使用我们喜欢的方式。但如果它低于24,我们需要避免使用,juts给出正常颜色或一种颜色而不是颜色等级混合。
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}
给你的线性布局提供id并设置背景,如果它没问题
qazxswpoi
在我的情况下,我将指定我在布局文件中使用的库UI组件的完整包名称。
对我来说,这是因为我试图将xml矢量图形图像与普通png混合在同一个布局文件中,
只有在我用pngs替换了xml矢量图形之后才开始工作
如果您使用支持库使用app:srcCompat
并忘记使用android:src
而不是See that link,也会发生这种情况
确切的错误是:二进制XML文件行#XX:错误膨胀类ImageView
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();
}
我知道这个问题已经得到了回答,但我仍然认为有人会遇到这类问题。
在我的情况下问题是我正在将我的应用程序加载到手机,它从res / layout /文件夹中引用布局,并从res / values / dimens引用@dimens的值,这是它尝试访问的font_22,它在res / values-xlarge中定义/梦诗。
我实际上正在更新现有项目的UI。
我遇到了这个问题,因为我正在使用IDE Eclipse,我在ctrl + space中提示提示,同时为布局文件夹编写xml,它显示值和值-xlarge文件夹中的所有值,无论我写的是哪个文件夹。
我也知道两个文件中的值应该相同,以映射到不同的屏幕。
希望这可以帮助有人遇到这种愚蠢的问题。
在仅使用一个构造函数创建自定义视图时遇到了同样的错误,尝试为自定义视图定义所有构造函数。
View.inflate(getApplicationContext(), R.layout.my_layout, null)
我遇到了同样的bug,发现根本原因是:
使用应用程序上下文来扩充视图。
充满活动上下文修复了错误。
我有这个错误,因为我选择主题作为材料主题。但是当我试图在4.4.2上运行app时,它给出了这个错误。
解决方案:选择Theme_holo作为主题
我的图片大小只有14Kb,但问题是设计师给了我1011px x 1819px(分辨率太高因此导致InflateException)