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 com.google.android.material.chip.Chip
    Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class com.google.android.material.chip.Chip
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at renetik.android.viewbase.CSView.inflate(CSView.java:104)
        at renetik.android.viewbase.CSView.getView(CSView.java:119)
        at renetik.android.viewbase.CSViewController.getView(CSViewController.java:405)
        at renetik.android.viewbase.CSViewController.getView(CSViewController.java:408)
        at renetik.android.viewbase.CSView.findView(CSView.java:108)
        at renetik.android.extensions.view.CSViewKt.editText(CSView.kt:22)
        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 renetik.android.extensions.view.ViewKt$onClick$$inlined$apply$lambda$1.onClick(View.kt:51)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24697)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.UnsupportedOperationException: Do not set the background; Chip manages its own background drawable.
        at com.google.android.material.chip.Chip.setBackground(Chip.java:397)
        at android.view.View.<init>(View.java:5200)
        at android.widget.TextView.<init>(TextView.java:824)
        at android.widget.Button.<init>(Button.java:172)
        at android.widget.CompoundButton.<init>(CompoundButton.java:94)
        at android.widget.CheckBox.<init>(CheckBox.java:69)
        at android.widget.CheckBox.<init>(CheckBox.java:65)
        at androidx.appcompat.widget.AppCompatCheckBox.<init>(AppCompatCheckBox.java:64)
        at com.google.android.material.chip.Chip.<init>(Chip.java:169)

In layout just something like this…

  <com.google.android.material.chip.Chip
            android:id="@+id/EditHouse_Category_Vacant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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>
    </style>

</resources>

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 {
        super.setBackground(background)
    } catch (ex: UnsupportedOperationException) {
        warn(ex)
    }

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

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(), R.style.CSNavigationContainer))

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(R.style.CSNavigationContainer) }

now it works with the same style.

Leave a Reply

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