Android 多个主题切换使用attr属性导致报错问题分析

贴一下关键报错信息,方便有问题的童鞋搜索

Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010009 a=-1}

这里主要讨论attr属性问题,所以主题切换我随手写了个比较简单的。(解决方案在文章末尾)

问题

在Android5.0一下的系统在xml中使用?attr/button_bg的方式指定颜色的时候会导致crash,我们先来看demo。
我写了两套主题,一套日间模式和一套夜间模式,然后通过setTheme来切换。

demo

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme_Day" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="appbarBg">#ffffff</item>
    </style>

    <style name="AppTheme_Night" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#181818</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">#123456</item>
        <item name="appbarBg">#123456</item>
    </style>
</resources>

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="appbarBg" format="reference|color" />
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/appbarBg">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

    </android.support.design.widget.AppBarLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="100dp"
        android:onClick="changeTheme"
        android:background="@drawable/button_bg"
        android:text="切换主题" />

</LinearLayout>

button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp"/>
    <solid android:color="?attr/appbarBg" />
</shape>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(Util.THEME);
        setContentView(R.layout.activity_main);
    }

    public void changeTheme(View view) {
        Util.THEME = Util.THEME == R.style.AppTheme_Day? R.style.AppTheme_Night: R.style.AppTheme_Day;
        recreate();
    }
}

demo在Android5.0以上跑是没有问题的
但是5.0以下会打开app会直接报错
问题出在文件button_bg.xml上面

  <solid android:color="?attr/appbarBg" />

这一句导致的报错

原因:attr属性在5.0以前不支持直接在shape,selector等drawable文件里面使用。

解决方案:

分别写两个drawable文件,然后在attr文件中注册对应的属性,然后在通过style里面的主题来分别定义两个drawable。

新建两个drawable

button_bg_light.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp"/>
    <solid android:color="#ffffff" />
</shape>

button_bg_night.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp"/>
    <solid android:color="#123456" />
</shape>

attr.xml 加一句

<attr name="button_bg" format="reference" />

style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme_Day" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="appbarBg">#ffffff</item>
        <item name="button_bg">button_bg_light</item>
    </style>

    <style name="AppTheme_Night" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#181818</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">#123456</item>
        <item name="appbarBg">#123456</item>
        <item name="button_bg">button_bg_night</item>
    </style>
</resources>

activity_main.xml

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="100dp"
        android:onClick="changeTheme"
        android:background="?attr/button_bg"
        android:text="切换主题" />

这样就避免了在drawable文件里面引用attr属性导致的报错问题。
ps:如果还有问题可以加qq群:295456349

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容