androidx – material android Chip is crashing when inflated in xml layout-ThrowExceptions

Exception or error:

Even if I don’t do anything in code… looks like somewhere in view class drawable is created somehow and this crashes Chip as this is not allowed… Any help welcome.

android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class
    Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(
        at android.view.LayoutInflater.createView(
        at android.view.LayoutInflater.createViewFromTag(
        at android.view.LayoutInflater.createViewFromTag(
        at android.view.LayoutInflater.rInflate(
        at android.view.LayoutInflater.rInflateChildren(
        at android.view.LayoutInflater.rInflate(
        at android.view.LayoutInflater.rInflateChildren(
        at android.view.LayoutInflater.inflate(
        at android.view.LayoutInflater.inflate(
        at com.tanya.renetik.redriver.view.route.EditHouseFieldsController.<init>(EditHouseFieldsController.kt:15)
        at com.tanya.renetik.redriver.view.route.started.StartedAddHouseController.<init>(StartedAddHouseController.kt:31)
        at com.tanya.renetik.redriver.view.route.started.StartedRouteMapController$onCreate$2.invoke(StartedRouteMapController.kt:37)
        at com.tanya.renetik.redriver.view.route.started.StartedRouteMapController$onCreate$2.invoke(StartedRouteMapController.kt:17)
        at android.view.View.performClick(
        at android.view.View$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invoke(Native Method)
     Caused by: java.lang.UnsupportedOperationException: Do not set the background; Chip manages its own background drawable.
        at android.view.View.<init>(
        at android.widget.TextView.<init>(
        at android.widget.Button.<init>(
        at android.widget.CompoundButton.<init>(
        at android.widget.CheckBox.<init>(
        at android.widget.CheckBox.<init>(
        at androidx.appcompat.widget.AppCompatCheckBox.<init>(

In layout just something like this…

            android:text="@string/Category_Vacant" />

My theme file is simple:

    <style name="DriverTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/driver_primary</item>
        <item name="colorPrimaryDark">@color/driver_primary_dark</item>
        <item name="colorSecondary">@color/driver_secondary</item>


ext.kotlin_version = ‘1.3.0’
ext.material_version = ‘1.0.0’
ext.appcompat_version = ‘1.0.0’
ext.play_version = ‘16.0.0’

How to solve:

Just my small workaround after one day of battling :/

class ChipCrashWorkaround(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
    : Chip(context, attrs, defStyleAttr) {

    constructor (context: Context, attrs: AttributeSet?)
            : this(context, attrs, R.attr.chipStyle)

    constructor (context: Context) : this(context, null)

    override fun setBackground(background: Drawable) = try {
    } catch (ex: UnsupportedOperationException) {

    override fun setBackgroundDrawable(background: Drawable) = try {
    } catch (ex: UnsupportedOperationException) {

Well I can see clearly that somehow drawable is applied probably from theme to my chip even if I disable all customizations everywhere.. Wtf I don’t know but have to move forward…


Which version of the library are you using? The exception is being thrown because chip manages its own background and doesn’t support View#setBackground. Is it possible that a parent of your chip instance is trying to set the chip’s background?


Ok I found it, I was using this code to create top level layout:

view = FrameLayout(ContextThemeWrapper(this.context(),

And inside of this layout I have set white background what should be ok but…
Now I see what this ContextThemeWrapper actually do:

The specified theme will be applied on top of the base context's theme.

So by this my context theme was modified to contain background with white color and this somehow went to other views created later with this context and this crashed Chip as it don’t allow background.

So I have to stop using ContextThemeWrapper , maybe I misunderstood real purpose, I wanted to apply style to my view programmatically, what actually worked, but it modified whole theme.

So I found library paris for aplying styles programatically and changed that code to:

  view = FrameLayout(context()).apply { style( }

now it works with the same style.

Leave a Reply

Your email address will not be published. Required fields are marked *