Binary XML file line 2: Error inflating class

Posted 陈蒙_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Binary XML file line 2: Error inflating class相关的知识,希望对你有一定的参考价值。

文章目录


在开发中遇到一个神奇的坑,先做记录。

问题

功能很简单,给某个点击区域设置按压态,正常颜色是 @color/colorPrimaryDark,被点击时是 @color/colorPrimary,用 selector 实现,具体代码如下:

MainActivity:

public class MainActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    tools:context=".MainActivity">
</android.support.constraint.ConstraintLayout>

background.xml 放在 res/color 目录下,内容为:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_pressed="true" />
    <item android:color="@color/colorPrimaryDark" />
</selector>

在 Android 10 上运行正常,但是在 Android 9 版本上崩溃,堆栈日志如下:

java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.bgdrawabledemo/com.example.bgdrawabledemo.MainActivity: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:658)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.bgdrawabledemo.MainActivity.onCreate(MainActivity.java:11)
        at android.app.Activity.performCreate(Activity.java:7458)
        at android.app.Activity.performCreate(Activity.java:7448)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.bgdrawabledemo:color/background with resource ID #0x7f04001a
     Caused by: android.content.res.Resources$NotFoundException: File res/color/background.xml from drawable resource ID #0x7f04001a
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1166)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:917)
        at android.content.res.Resources.loadDrawable(Resources.java:1083)
2020-05-27 14:16:49.102 24089-24089/com.example.bgdrawabledemo E/AndroidRuntime:     at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:1010)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:985)
        at android.view.View.<init>(View.java:5050)
        at android.view.ViewGroup.<init>(ViewGroup.java:659)
        at android.view.ViewGroup.<init>(ViewGroup.java:655)
        at android.view.ViewGroup.<init>(ViewGroup.java:651)
        at android.support.constraint.ConstraintLayout.<init>(ConstraintLayout.java:575)
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:658)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.bgdrawabledemo.MainActivity.onCreate(MainActivity.java:11)
        at android.app.Activity.performCreate(Activity.java:7458)
        at android.app.Activity.performCreate(Activity.java:7448)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
        at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:190)
        at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:148)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
        at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:1144)
        	... 35 more

解决方案

凭着经验摸索出了解决方案,即:

  • background.xml 由 res/color 目录移至 res/drawable 目录;
  • 布局文件引用改为 android:background="@drawable/background"
  • background.xml 内容修改为如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimary" android:state_pressed="true" />
    <item android:drawable="@color/colorPrimaryDark" />
</selector>

具体原因暂未知,有懂得大神请不吝赐教。

以上是关于Binary XML file line 2: Error inflating class的主要内容,如果未能解决你的问题,请参考以下文章

Binary XML file line 2: Error inflating class

在测试类中膨胀 ViewBinding 时出错:Binary XML file line #38: Binary XML file line #38: Error inflating class &l

Binary XML file line #8 in 包名路经:layout/layout文件名字: Binary XML file line #8 in 包名路经:layout/layout文件名字

Binary XML file line #17<vector; tag requires viewportWidth ;0

Binary XML file line #17<vector; tag requires viewportWidth ;0

android.view.InflateException: Binary XML file line #7: Error inflating class(OOM)