低版本Android设备加载AppBarLayout时资源找不到异常问询
解决低版本Android机型中AppBarLayout引发的Resources$NotFoundException问题
嘿,我之前碰到过一模一样的坑!这大概率是Design Support Library在低版本Android设备上的兼容性bug,或者是主题配置触发了系统去加载你应用里根本没有的资源。咱们一步步来搞定它:
问题拆解
从异常栈能看出来,系统初始化AppBarLayout时,试图加载一个叫ic_download_active.png的动画资源,但你的应用里没这个文件,直接触发了Resources$NotFoundException,进而导致布局加载失败的InflateException。核心问题出在StateListAnimator的加载流程上,低版本机型处理这类动画资源时容易出现这种“找错资源”的情况。
异常栈详情
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi-v4/ic_download_active.png from xml type anim resource ID #0x7f020000 at android.content.res.Resources.loadXmlResourceParser(Resources.java:2817) at android.content.res.Resources.loadXmlResourceParser(Resources.java:2772) at android.content.res.Resources.getAnimation(Resources.java:1113) at android.animation.AnimatorInflater.loadStateListAnimator(AnimatorInflater.java:165) at android.support.design.widget.ViewUtilsLollipop.setStateListAnimatorFromAttrs(ViewUtilsLollipop.java:50) at android.support.design.widget.AppBarLayout.<init>(AppBarLayout.java:183) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:611)
解决方案
1. 补上缺失的资源文件
虽然你的应用没用到这个图标,但Design Support Library的某些默认动画逻辑可能偷偷引用了它。你可以:
- 从AppCompat/Design库的资源包中找到
ic_download_active.png,复制到你项目的drawable-hdpi、drawable-mdpi等对应dpi目录; - 或者自己做一个同名的透明占位图标,让系统能找到“存在”的资源就行。
2. 直接禁用AppBarLayout的StateListAnimator
既然问题出在这个动画加载上,咱们直接把它关掉就好了:
- XML配置方式:给AppBarLayout添加一个属性:
android:stateListAnimator="@null"
修改后的AppBarLayout代码:
<android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:background="?attr/colorPrimary" android:theme="@style/Theme.AppCompat" android:layout_width="match_parent" android:layout_height="wrap_content" android:stateListAnimator="@null"> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" app:popupTheme="@style/Theme.AppCompat" android:layout_height="@dimen/f_tabbar_tab_prefered_height" android:background="?attr/colorPrimary" app:tabGravity="center" app:tabMode="scrollable" /> </android.support.design.widget.AppBarLayout>
- 代码动态设置:在Activity的
onCreate里初始化AppBarLayout后执行:
AppBarLayout appBar = findViewById(R.id.app_bar); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { appBar.setStateListAnimator(null); }
3. 升级Design Support Library版本
旧版本的Design库确实存在这类低版本兼容性bug,你可以把依赖升级到较新的稳定版本(如果已经迁移到AndroidX就用Material库):
- 旧Support库版本:
implementation 'com.android.support:design:28.0.0'
- AndroidX版本:
implementation 'com.google.android.material:material:1.4.0'
4. 检查主题配置
确保你的AppTheme和AppBarLayout的主题没有引用到需要这个图标的自定义属性。比如确认Theme.AppCompat的继承逻辑正确,有没有在主题里设置过相关动画属性,导致系统误加载资源。
你提供的参考代码
布局代码
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:id="@+id/userdata_item_tabs" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:background="?attr/colorPrimary" android:theme="@style/Theme.AppCompat" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" app:popupTheme="@style/Theme.AppCompat" android:layout_height="@dimen/f_tabbar_tab_prefered_height" android:background="?attr/colorPrimary" app:tabGravity="center" app:tabMode="scrollable" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/items_delete_btn" android:layout_below="@id/app_bar" /> <Button android:id="@+id/user_items_delete_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_gravity="center" android:gravity="center" android:text="Delete" android:visibility="visible" /> </RelativeLayout> </android.support.design.widget.CoordinatorLayout>
AndroidManifest中Activity配置
<activity android:name=".activity.MapsAppActivity" android:configChanges="orientation|screenSize" android:theme="@style/AppTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
样式代码
<style name="AppTheme" parent="Theme.AppCompat"></style>
内容的提问来源于stack exchange,提问作者Vinay Kumar
相关产品推荐
相关产品推荐

