Angga Ari Wijaya Angga Ari Wijaya - 7 months ago 274
Java Question

Error android.view.InflateException: Binary XML: Error inflating class Button caused by xml background

Couple days i'm confused with error Caused by: android.view.InflateException: Binary XML file line #46: Error inflating class Button, i tried to find out what happen but no luck, in similar problem i found in stackoverflow but i think my case is little bit difference,,

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo
android.view.InflateException: Binary XML file line #46: Error inflating class Button
Caused by: android.content.res.Resources$NotFoundException:
**File res/drawable/btn_primary.xml from drawable resource ID #0x7f020058**


then i realize, my file btn_primary_dark.xml and another drawable xml caused this error, everything works fine on Lollipop and up, but not at API before 21 like kitkat or JB,,in stack trace write not found resource but i have my xml file, i have file on
drawable
folder

btn_primary.xml
btn_primary_dark.xml
btn_light.xml
..


and folder
drawable-v21


btn_primary.xml
btn_primary_dark.xml
btn_light.xml
..


btn_primary.xml
in
drawable
folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<solid android:color="?colorControlHighlight" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<solid android:color="@color/primary" />
</shape>
</item>
</selector>


btn_primary.xml
in
drawable-v21
folder

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?colorControlHighlight">
<item>
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<solid android:color="@color/primary" />
</shape>
</item>
</ripple>


please if someone ever has same issue, i want to know how i solve this..
thanks for your help.

Answer

i figure out something, turns out error because xml pre lollipop doesn't recognize <solid android:color="?colorControlHighlight" /> so i just change with <solid android:color="@color/accent" />

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="3dip" />
            <solid android:color="@color/accent" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="3dip" />
            <solid android:color="@color/primary" />
        </shape>
    </item>
</selector>