Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android support library v4:22+ crashing pre Lollipop on attr/ in drawables

I've experienced a strange effect dealing with shape drawables with support lib attributes. I have following code, which crashes every time during inflation.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
<size android:width="26dp" android:height="26dp"/>
<solid android:color="?attr/colorPrimary"/>

Note I have used ?attr/colorPrimary as color. If I use

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
<size android:width="26dp" android:height="26dp"/>
<solid android:color="@color/primary"/>

It is working fine without any crashes. Issue is only in devices running lower version of Android than 5.0. I am using following support lib

compile 'com.android.support:support-v4:22.2.1'

Anyone found reason why this is happening? Is this a bug in support library?

like image 736
Michal Avatar asked Jul 28 '15 09:07

Michal


1 Answers

<solid android:color="?attr/colorPrimary"/> Points to a private color (was not public) in Android code, maybe it doesn't exist in some API.
While <solid android:color="@color/primary"/> will point to a color in your project, maybe you have a color name primary only in folder values-v21 so it's only crashing in versions below 5.0
I think you should try using this: <solid android:color="@android:attr/colorPrimary"/> to make sure the attribute exists.
Hope this helps.

like image 90
justHooman Avatar answered Oct 26 '22 17:10

justHooman