最近在调试程序的时候发现一个比较诡异的问题。
E FATAL EXCEPTION: main
Process: com.eathemeat.justplayer, PID: 22047
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eathemeat.justplayer/com.eathemeat.justplayer.play.PlayActivity}: android.view.InflateException: Binary XML file line #82 in com.eathemeat.justplayer:layout/activity_play: Binary XML file line #82 in com.eathemeat.justplayer:layout/activity_play: Error inflating class com.eathemeat.widget.slide.NiftySlider
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4034)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4187)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:99)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2632)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:222)
at android.os.Looper.loop(Looper.java:314)
at android.app.ActivityThread.main(ActivityThread.java:8690)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:565)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
Caused by: android.view.InflateException: Binary XML file line #82 in com.eathemeat.justplayer:layout/activity_play: Binary XML file line #82 in com.eathemeat.justplayer:layout/activity_play: Error inflating class com.eathemeat.widget.slide.NiftySlider
Caused by: android.view.InflateException: Binary XML file line #82 in com.eathemeat.justplayer:layout/activity_play: Error inflating class com.eathemeat.widget.slide.NiftySlider
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:879)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1035)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:986)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1165)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1126)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1168)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1126)
at android.view.LayoutInflater.inflate(LayoutInflater.java:707)
at android.view.LayoutInflater.inflate(LayoutInflater.java:545)
at com.eathemeat.justplayer.databinding.ActivityPlayBinding.inflate(ActivityPlayBinding.java:78)
at com.eathemeat.justplayer.databinding.ActivityPlayBinding.inflate(ActivityPlayBinding.java:72)
at com.eathemeat.justplayer.play.PlayActivity.onCreate(PlayActivity.kt:43)
at android.app.Activity.performCreate(Activity.java:8886)
at android.app.Activity.performCreate(Activity.java:8851)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1470)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4016)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4187)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:99)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2632)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:222)
at android.os.Looper.loop(Looper.java:314)
at android.app.ActivityThread.main(ActivityThread.java:8690)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:565)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
Caused by: android.content.res.Resources$NotFoundException: File res/color/default_track_color.xml from ComplexColor resource ID #0x7f050035
at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:1286)
at android.content.res.ResourcesImpl.loadComplexColorFromName(ResourcesImpl.java:1112)
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1191)
at android.content.res.Resources.getColorStateList(Resources.java:1195)
at androidx.core.content.res.ResourcesCompat$Api23Impl.getColorStateList(ResourcesCompat.java:687)
at androidx.core.content.res.ResourcesCompat.getColorStateList(ResourcesCompat.java:246)
at androidx.core.content.ContextCompat.getColorStateList(ContextCompat.java:508)
at androidx.appcompat.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:48)
at com.eathemeat.widget.slide.AbsSlider.processAttributes(AbsSlide.kt:292)
at com.eathemeat.widget.slide.AbsSlider.<init>(AbsSlide.kt:243)
at com.eathemeat.widget.slide.NiftySlider.<init>(NiftySlider.kt:16)
at com.eathemeat.widget.slide.NiftySlider.<init>(NiftySlider.kt:15)
at com.eathemeat.widget.slide.NiftySlider.<init>(Unknown Source:11)
... 30 more
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 0 to color: type=0x1, theme={InheritanceMap=[id=0x7f10022bcom.eathemeat.justplayer:style/Theme.JustPlayer, id=0x7f10025acom.eathemeat.justplayer:style/Theme.MaterialComponents.DayNight.NoActionBar.Bridge, id=0x7f100275com.eathemeat.justplayer:style/Theme.MaterialComponents.NoActionBar.Bridge, id=0x7f100249com.eathemeat.justplayer:style/Theme.MaterialComponents.Bridge, id=0x7f100066com.eathemeat.justplayer:style/Base.Theme.MaterialComponents.Bridge, id=0x7f100092com.eathemeat.justplayer:style/Base.V14.Theme.MaterialComponents.Bridge, id=0x7f10013ccom.eathemeat.justplayer:style/Platform.MaterialComponents, id=0x7f10020fcom.eathemeat.justplayer:style/Theme.AppCompat, id=0x7f10004bcom.eathemeat.justplayer:style/Base.Theme.AppCompat, id=0x7f1000b8com.eathemeat.justplayer:style/Base.V28.Theme.AppCompat, id=0x7f1000b5com.eathemeat.justplayer:style/Base.V26.Theme.AppCompat, id=0x7f1000afcom.eathemeat.justplayer:style/Base.V23.Theme.AppCompat, id=0x7f1000adcom.eathemeat.justplayer:style/Base.V22.Theme.AppCompat, id=0x7f1000a1com.eathemeat.justplayer:style/Base.V21.Theme.AppCompat, id=0x7f1000bacom.eathemeat.justplayer:style/Base.V7.Theme.AppCompat, id=0x7f10013acom.eathemeat.justplayer:style/Platform.AppCompat, id=0x7f100145com.eathemeat.justplayer:style/Platform.V25.AppCompat, id=0x103022eandroid:style/Theme.Material.NoActionBar, id=0x1030224android:style/Theme.Material, id=0x1030005android:style/Theme], Themes=[com.eathemeat.justplayer:style/Theme.JustPlayer, forced, com.eathemeat.justplayer:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
at android.content.res.TypedArray.getColor(TypedArray.java:538)
at android.content.res.ColorStateList.inflate(ColorStateList.java:366)
at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:296)
at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:1279)
... 42 more
原因是:android.content.res.Resources$NotFoundException: File res/color/default_track_color.xml from ComplexColor resource ID #0x7f050035这个资源找不到,这个资源是在一个UI控件的aar中,然后去看了下aar和apk中其实都是有这个xml的文件的。
解决:在网上找了很多的资料,有很多的原因:
- android问题的(查了下源码,在找xml文件和解析的时候出现错误都会包这个问题)
- 打包问题,在apk和aar中都有这个文件。排除
- android版本问题,有说是api24以下有getcolorstatelist的时候会出现这类问题。
搞了很久,还没没有办法从log上定位问题的原因。于是从自身出发,在这文件中查询问题:
试了很多的方法,最终定位问题:
//之前的代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@attr/colorPrimary" android:state_enabled="true" />
<item android:alpha="0.32" android:color="@android:color/black" />
</selector>
//修改后的代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_enabled="true" />
<item android:alpha="0.32" android:color="@android:color/black" />
</selector>
根本原因就是@和?的差别。在解析xml的时候由于无法定位主题中
@代表引用资源
? 代表引用主题属性
这2者会比较容易搞错。但是在lint中并不会检查这个问题。