Binary XML file line #2: Error inflating class <unknown>

1.背景

之前一直用的8.0版本模拟器没啥问题,偶然间切换到了android 6.0 版本的模拟器,发现我的应用崩了。

2.错误堆栈

01-26 02:26:28.670 3682-3682/pers.hl.library.debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: pers.hl.library.debug, PID: 3682
    java.lang.RuntimeException: Unable to start activity ComponentInfo{pers.hl.library.debug/pers.hl.module_login.LoginActivity}: java.lang.reflect.InvocationTargetException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at pers.hl.common.base.BaseVBActivity.initRootView(BaseVBActivity.kt:48)
        at pers.hl.common.base.BaseVBActivity.init(BaseVBActivity.kt:32)
        at pers.hl.common.base.BaseVBActivity.onCreate(BaseVBActivity.kt:27)
        at pers.hl.common.base.BaseVBVMActivity.onCreate(BaseVBVMActivity.kt:23)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:70)
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:64)
        at java.lang.reflect.Method.invoke(Native Method) 
        at pers.hl.common.base.BaseVBActivity.initRootView(BaseVBActivity.kt:48) 
        at pers.hl.common.base.BaseVBActivity.init(BaseVBActivity.kt:32) 
        at pers.hl.common.base.BaseVBActivity.onCreate(BaseVBActivity.kt:27) 
        at pers.hl.common.base.BaseVBVMActivity.onCreate(BaseVBVMActivity.kt:23) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class 
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:70) 
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:64) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at pers.hl.common.base.BaseVBActivity.initRootView(BaseVBActivity.kt:48) 
        at pers.hl.common.base.BaseVBActivity.init(BaseVBActivity.kt:32) 
        at pers.hl.common.base.BaseVBActivity.onCreate(BaseVBActivity.kt:27) 
        at pers.hl.common.base.BaseVBVMActivity.onCreate(BaseVBVMActivity.kt:23) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:619)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:70) 
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:64) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at pers.hl.common.base.BaseVBActivity.initRootView(BaseVBActivity.kt:48) 
        at pers.hl.common.base.BaseVBActivity.init(BaseVBActivity.kt:32) 
        at pers.hl.common.base.BaseVBActivity.onCreate(BaseVBActivity.kt:27) 
        at pers.hl.common.base.BaseVBVMActivity.onCreate(BaseVBVMActivity.kt:23) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 98724108 byte allocation with 1951802 free bytes and 93MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
        at android.content.res.Resources.loadDrawable(Resources.java:2540)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
        at android.view.View.(View.java:3948)
        at android.view.ViewGroup.(ViewGroup.java:573)
        at android.view.ViewGroup.(ViewGroup.java:569)
        at android.view.ViewGroup.(ViewGroup.java:565)
        at androidx.constraintlayout.widget.ConstraintLayout.(ConstraintLayout.java:568)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:70) 
        at pers.hl.module_login.databinding.LoginActivityLoginBinding.inflate(LoginActivityLoginBinding.java:64) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at pers.hl.common.base.BaseVBActivity.initRootView(BaseVBActivity.kt:48) 
        at pers.hl.common.base.BaseVBActivity.init(BaseVBActivity.kt:32) 
        at pers.hl.common.base.BaseVBActivity.onCreate(BaseVBActivity.kt:27) 
        at pers.hl.common.base.BaseVBVMActivity.onCreate(BaseVBVMActivity.kt:23) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3.分析

开始就是一顿花里胡哨的瞎分析,因为我这里是反射调用ViewBinding的inflate方法,就去各种改代码,然并卵。。。

简单查看后,抓住一句重要的信息:Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class

这意思可不就是在我的页面布局xml文件里面第二行报错么,于是打开了我的布局,第二行就是个ConstraintLayout呀,没啥特别的。

又着急了,谷哥度娘问了一圈,多方查阅资料,有告诉我是啥drawable资源没放对位置的原因,有说是自定义view没写对的原因,还有啥删除v24的文件夹之类的。最后就把此布局只保留一个根layout,然,无果。。。

实在想不通啊,又到群里找大佬帮忙,可惜大佬没理我。

最后冷静下来,仔细看了看布局,这不起眼的一行引起了我的注意:

Binary XML file line #2: Error inflating class <unknown>_第1张图片

点开一看,这是一个.jpg格式的图片,我放在drawable下面的。

 

4.解决

结合上面报错最后一个原因跟loadDrawable有点关系,抱着试试看的态度删除此行。

然后,好了,神奇的Android世界。。

 

5.原理

最后还是没想通是为啥,自己分析可能是不同版本的inflate方法差异,想去看源码,时间不允许,就留到下次吧。

你可能感兴趣的:(源码分析,安卓开发,errors)