title: Testing fragment: I keep getting java.lang.RuntimeException: android.view.InflateException
body: <p>I was following this <a href="https://codelabs.developers.google.com/codelabs/mdc-101-kotlin/index.html#0" rel="noreferrer">codelab</a> about Material Components and after finishing it I thought I should practice writing some instrumentation tests. The app works fine. But I keep getting this error when I run any of the tests: </p>
  
  <pre><code>    java.lang.RuntimeException: android.view.InflateException: Binary XML file line #37: Binary 
      XML file line #37: Error inflating class 
      com.google.android.material.textfield.TextInputLayout
      at androidx.test.runner.MonitoringInstrumentation.runOnMainSync(MonitoringInstrumentation.java:450)
      at androidx.test.core.app.ActivityScenario.onActivity(ActivityScenario.java:564)
      at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:300)
      at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:282)
      at com.google.codelabs.mdc.kotlin.shrine.LoginFragmentTest.username_and_password_editTexts_visible(LoginFragmentTest.kt:64)
      at java.lang.reflect.Method.invoke(Native Method)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.junit.runners.Suite.runChild(Suite.java:128)
      at org.junit.runners.Suite.runChild(Suite.java:27)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
      at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
      at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
      at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1941)
      Caused by: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class com.google.android.material.textfield.TextInputLayout
      Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class com.google.android.material.textfield.TextInputLayout
      Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Constructor.newInstance0(Native Method)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
      at android.view.LayoutInflater.createView(LayoutInflater.java:645)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
      at com.google.codelabs.mdc.kotlin.shrine.LoginFragment.onCreateView(LoginFragment.kt:19)
      at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
      at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
      at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
      at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
      at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
      at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
      at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
      at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
      at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
      at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
      at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1818)
      at androidx.fragment.app.BackStackRecord.commitNow(BackStackRecord.java:297)
      at androidx.fragment.app.testing.FragmentScenario$1.perform(FragmentScenario.java:317)
      at androidx.fragment.app.testing.FragmentScenario$1.perform(FragmentScenario.java:301)
      at androidx.test.core.app.ActivityScenario.lambda$onActivity$2$ActivityScenario(ActivityScenario.java:551)
      at androidx.test.core.app.ActivityScenario$$Lambda$4.run(Unknown Source)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1959)
      at android.os.Handler.handleCallback(Handler.java:751)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6236)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
      Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
      at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:248)
      at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:222)
      at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:150)
      at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:120)
      at com.google.android.material.textfield.TextInputLayout.&lt;init&gt;(TextInputLayout.java:424)
      at com.google.android.material.textfield.TextInputLayout.&lt;init&gt;(TextInputLayout.java:396)
      ... 38 more
  
      Test running failed: Instrumentation run failed due to 'Process crashed.'
  </code></pre>
  
  <p><strong>My gradle dependencies:</strong> </p>
  
  <pre><code>dependencies {
      implementation 'com.google.android.material:material:1.1.0'
      implementation 'androidx.appcompat:appcompat:1.1.0'
      implementation 'com.android.volley:volley:1.1.1'
      implementation 'com.google.code.gson:gson:2.8.6'
      implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
      testImplementation 'junit:junit:4.13'
      androidTestImplementation 'androidx.test:core:1.2.0'
      androidTestImplementation 'androidx.test.ext:junit:1.1.1'
      androidTestImplementation 'androidx.test:runner:1.3.0-alpha04'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha04'
      debugImplementation ('androidx.fragment:fragment-testing:1.2.2'){
          exclude group: 'androidx.test', module : 'core'
      }
  }
  </code></pre>
  
  <p>I tried changing the Material dependency's version but with no luck.</p>
  
  <p><strong>My styles file</strong>:</p>
  
  <pre><code>    &lt;style name="AppTheme.NoActionBar" parent="Theme.MaterialComponents.Light.NoActionBar"&gt;
          &lt;!-- Customize your theme here. --&gt;
          &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt;
          &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt;
          &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt;
      &lt;/style&gt;
  </code></pre>
  
  <p><strong>Fragment</strong> layout file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/loginPageBackgroundColor"
      tools:context=".LoginFragment"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:clipChildren="false"
          android:clipToPadding="false"
          android:orientation="vertical"
          android:padding="24dp"
          android:paddingTop="16dp"&gt;
  
          &lt;ImageView
              android:layout_width="64dp"
              android:layout_height="64dp"
              android:layout_gravity="center_horizontal"
              android:layout_marginTop="48dp"
              android:layout_marginBottom="16dp"
              app:srcCompat="@drawable/shr_logo"
              android:contentDescription="@string/shr_logo_content_description"  /&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="132dp"
              android:text="@string/shr_app_name"
              android:textAllCaps="true"
              android:textSize="16sp" /&gt;
  
          &lt;com.google.android.material.textfield.TextInputLayout
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:layout_margin="4dp"
              android:hint="@string/shr_hint_username"
              style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
              &gt;
              &lt;com.google.android.material.textfield.TextInputEditText
                  android:id="@+id/username_edit_text"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  /&gt;
          &lt;/com.google.android.material.textfield.TextInputLayout&gt;
  
          &lt;com.google.android.material.textfield.TextInputLayout
              android:id="@+id/password_text_input"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="4dp"
              android:hint="@string/shr_hint_password"
              app:errorEnabled="true"
              style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
              android:inputType="textPassword"
              &gt;
              &lt;com.google.android.material.textfield.TextInputEditText
                  android:id="@+id/password_edit_text"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" /&gt;
          &lt;/com.google.android.material.textfield.TextInputLayout&gt;
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"&gt;
              &lt;com.google.android.material.button.MaterialButton
                  android:id="@+id/next_button"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:layout_alignParentEnd="true"
                  android:layout_alignParentRight="true"
                  android:text = "@string/shr_button_next"
                  /&gt;
              &lt;com.google.android.material.button.MaterialButton
                  android:id="@+id/cancel_button"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginEnd="12dp"
                  android:layout_marginRight="12dp"
                  android:layout_toStartOf="@id/next_button"
                  android:layout_toLeftOf="@id/next_button"
                  android:text="@string/shr_button_cancel"
                  style="@style/Widget.MaterialComponents.Button.TextButton"
                  /&gt;
          &lt;/RelativeLayout&gt;
  
          &lt;!-- Snippet from "Add buttons" section goes here. --&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>The code on line 37 is the first TextInputLayout</p>
  
  <p>The fragment implementation:</p>
  
  <pre><code>import android.os.Bundle
  import android.text.Editable
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import androidx.fragment.app.Fragment
  import kotlinx.android.synthetic.main.shr_login_fragment.*
  import kotlinx.android.synthetic.main.shr_login_fragment.view.*
  
  
  class LoginFragment : Fragment() {
  
      override fun onCreateView(
              inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          val view = inflater.inflate(R.layout.shr_login_fragment, container, false)
          view.next_button.setOnClickListener {
              if(isPasswordValid(password_edit_text.text)){
                  view.password_text_input.error = null
                  // the false so as to not return back to the login screen again
                  (activity as NavigationHost).navigateTo(ProductGridFragment(),false)
              }else{
                  view.password_text_input.error = getString(R.string.shr_error_password)
              }
          }
          view.password_edit_text.setOnKeyListener { _, _, _ -&gt;
              if(isPasswordValid(password_edit_text.text)){
                  view.password_text_input.error = null
              }
              false
          }
          view.cancel_button.setOnClickListener {
              view.password_edit_text.setText("")
              view.username_edit_text.setText("")
          }
          return view
      }
      private fun isPasswordValid(text: Editable?): Boolean{
          return text!=null &amp;&amp; text.length &gt;= 8
      }
  }
  </code></pre>