首页 文章

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

提问于
浏览
120

我收到了很多在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>

21 回答

  • 4

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

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

  • 0

    ViewFlipper 在布局膨胀期间将所有图像加载到内存中 . 因为我的图像很大,需要很多内存,我用 ImageSwitcher 替换了 ViewFlipper ,这可以用像 ViewFlipper 这样的动画来改变图像,但它当时只加载一个图像 .

  • 4

    如果您使用 VectorDrawable 使用支持库并忘记使用 app:srcCompat 而不是 android:src ,也会发生这种情况

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

    See that link

  • 9

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

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

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

    我遇到了这个问题,因为我正在使用IDE Eclipse,其中我在为布局文件夹编写xml时为了提示空间,它显示了值以及values-xlarge文件夹中的所有值,而不管我正在编写哪个文件夹 .

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

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

  • 0

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

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

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

  • 2

    This link可以帮到你 . 尝试检查清单中的问题 . 如果你可以再次发生它,发布你的整个堆栈跟踪,以便我们可以看到错误实际是什么 .

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

  • 24

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

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

    充满活动上下文修复了错误 .

  • 18

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

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

  • 238

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

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

    • 我在布局膨胀之前放了一个断点

    • 我以调试模式运行应用程序,直到它到达特定断点

    • 我用光标选择了该行并在其上运行了 Evaluate expression

    • Run > Evaluate Expressionalt - F8

    • 结果向我展示了有关问题根源的更多信息,在我的例子中,这是一个使用 tools:targetApi="lollipop" 的可绘制文件(该错误仅发生在较旧的设备上) .

  • 3

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

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

    通过用 this 替换 getApplicationContext() 来修复

  • 5

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

    Edit note: 如果您要从eclipse迁移到Android Studio并且您的项目来自eclipse可能会发生,所以小心在Android Studio中的东西与eclipse有点不同 .

  • 0

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

  • 0

    在我的情况下,当我使用 Floating action button 并设置 android:backgroundTint="#000" 时会发生此错误 . 然后就是不要设置 backgroundTint 并解决问题 . 希望它对你有所帮助 .

  • 1

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

    错误只是因为我试图使用一个: 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 !
     />
    

    我希望这有帮助 .

  • 4

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

    • XML中的布局标记错误

    • 将重资源直接加载到ImageView中导致OOM

    • 使用样式的问题 .

    在视图膨胀时,一个最被忽视的因素可能是 usage of correct Context . 请检查您是否在使用需要Activity上下文的ApplicationContext . 虽然,ApplicationContext可能不会总是出错,但根据您的视图层次结构,它可能是至关重要的 .

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

  • 1

    我遇到过同样的问题,我在xml而不是View中使用了view标签 . 替换为View类修复了该问题 .

    希望它对你有帮助..

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

    and how I fixed it ,实际上它似乎是Api问题所以我们需要检查api级别,如果它高于API 24,那么我们就可以按照我们喜欢的方式使用 . 但如果它低于24,我们需要避免使用,juts给出正常颜色或一种颜色而不是颜色等级混合 .

    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}
    
  • 0

    我知道这是一个已回答的问题,但我没有看到我面临的原因 .

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

    解决方案是将所有drawables移动到/ drawable文件夹(no -24) .

  • 3

    当我在API lvl 16上运行我的应用程序时,我遇到了同样的错误(26很好) . 这是因为我在我的形状drawable中使用 ?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>
    

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

  • 0

    在我的情况下,我将指定我在布局文件中使用的库UI组件的完整包名称 .

相关问题