SwitchCompat throwing error

Had the same error, ended up finding nothing and also landed here. Colleague suggested one small thing which fixed this Exception for me; so I wanted to leave this here:

The SwitchMaterial or SwitchCompat uses a custom defStyleAttr internally. So the default code where I always override the defStyleAttr with 0 when it is not supplied. This overwrites the Style in the parent component and then the crash occurs.

Changing my Constructor from:

class MySwitch(
  context: Context,
  attrs: AttributeSet? = null,
  defStyleAttr: Int = 0
) : SwitchMaterial(context, attrs, defStyleAttr)

to

class MySwitch(
  context: Context,
  attrs: AttributeSet? = null
) : SwitchMaterial(context, attrs)

solved the Problem for me


SwitchCompat requires that you set the android:textOn and android:textOff values unless you set app:showText="false":

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="ON"
    android:textOff="OFF" />

or (assuming you have xmlns:app="http://schemas.android.com/apk/res-auto" at the top of your XML file)

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:showText="false" />