title: android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class
body: <p>I'm currently facing an issue in my app, I've looked and tried many solutions, but I was unable to find any that actually works.</p>
  
  <p>I have an activity that contains a list of custom components. This components are android CardView elements wrapped in a LinearLayout.</p>
  
  <p>Each of this card components contains a Relative layout which is empty by default.</p>
  
  <p>When calling the component in the activity XML file, I add the custom attribute <code>app:partLayout="@layout/choice_access_part"</code>. This attribute is processed in my component code. After that, I inflate the layout passed in reference in partLayout attribute add I try to add it to the RelativeLayout in my component.</p>
  
  <pre><code>val inflatedLayout: View = inflater.inflate(partLayout, selectionContainer, false)
  selectionContainer.addView(inflatedLayout)
  </code></pre>
  
  <p>I have no problem compiling the application and running it until I reach this part of the code and get the error : <code>android.view.InflateException: Binary XML file line #32: Binary XML file line #32: Error inflating class</code> </p>
  
  <p>Component code</p>
  
  <pre><code>class MultipleChoiceCard(context: Context?, attrs: AttributeSet?) : CardView(context, attrs) {
  
      val title: TextView
      val nonMandatoryText: TextView
      val selectionContainer: RelativeLayout
  
      init {
          val inflater: LayoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
          inflater.inflate(R.layout.component_multiple_choice, this)
  
          // Getting elements in XML
          title = find(R.id.multiple_choice_title)
          nonMandatoryText = find(R.id.non_mandatory_text)
          selectionContainer = find(R.id.selection_container)
  
          // Extract value from XML
          val a = context.theme.obtainStyledAttributes(attrs, R.styleable.MultipleChoiceCard, 0, 0)
          val titleValue = a.getString(R.styleable.MultipleChoiceCard_title)
          val nonMandatoryTextValue = a.getString(R.styleable.MultipleChoiceCard_nonMandatoryText) ?: ""
          val colorValue = a.getInt(R.styleable.MultipleChoiceCard_cardColor, R.color.user_deep_orange)
          val partLayout = a.getInt(R.styleable.MultipleChoiceCard_partLayout, R.layout.choice_rythm_part)
  
          // Applying the XML values to the wanted elements
          title.text = titleValue
          title.setTextColor(colorValue)
          // Set the non mandatory text visible if the value is not an empty string
          if(nonMandatoryTextValue != ""){
              nonMandatoryText.visibility = View.VISIBLE
              nonMandatoryText.text = nonMandatoryTextValue
          }
  
          val inflatedLayout: View = inflater.inflate(partLayout, selectionContainer, false)
          selectionContainer.addView(inflatedLayout)
  
          a?.recycle()
  
      }
  
  }
  </code></pre>
  
  <p>Component XML file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.v7.widget.CardView
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:padding="20dp"
          android:id="@+id/rythm_card"
          app:cardBackgroundColor="#2d292a"&gt;
  
          &lt;RelativeLayout
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:padding="20dp"&gt;
  
              &lt;TextView
                  android:id="@+id/multiple_choice_title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textAllCaps="true"
                  android:textColor="@color/user_deep_orange"
                  android:layout_marginBottom="10dp"
                  android:textStyle="bold"&gt;
              &lt;/TextView&gt;
  
              &lt;TextView
                  android:id="@+id/non_mandatory_text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/multiple_choice_title"
                  android:textColor="#8c8f91"
                  android:textSize="13sp"
                  android:textStyle="italic"
                  android:visibility="gone"
                  android:layout_marginBottom="10dp"&gt;
              &lt;/TextView&gt;
  
              &lt;RelativeLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/non_mandatory_text"
                  android:id="@+id/selection_container"&gt;
  
              &lt;/RelativeLayout&gt;
  
          &lt;/RelativeLayout&gt;
  
      &lt;/android.support.v7.widget.CardView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Layout passed as attribute</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;RadioGroup
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/non_mandatory_text"
          android:id="@+id/rythm_radio_group"&gt;
  
          &lt;RadioButton
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:text="Libre"
              android:textSize="20sp"
              android:layoutDirection="rtl"
              android:buttonTint="@color/user_deep_orange"
              android:layout_marginBottom="10dp"&gt;
  
          &lt;/RadioButton&gt;
  
          &lt;RadioButton
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:text="Toutes les 10 secondes"
              android:textSize="20sp"
              android:layoutDirection="rtl"
              android:buttonTint="@color/user_deep_orange"
              android:layout_marginBottom="10dp"&gt;
  
          &lt;/RadioButton&gt;
  
          &lt;RadioButton
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:text="Toutes les 30 secondes"
              android:textSize="20sp"
              android:layoutDirection="rtl"
              android:buttonTint="@color/user_deep_orange"
              android:layout_marginBottom="10dp"&gt;
  
          &lt;/RadioButton&gt;
  
          &lt;RadioButton
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:text="Toutes les minutes"
              android:textSize="20sp"
              android:layoutDirection="rtl"
              android:buttonTint="@color/user_deep_orange"&gt;
  
          &lt;/RadioButton&gt;
  
      &lt;/RadioGroup&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>