android.support.design.button.MaterialButton throws InflateException

Nabster picture Nabster · Aug 14, 2018 · Viewed 17.2k times · Source

I am trying to follow the The MDC Codelab on codelabs.developers.google.com with the latest library versions. The EditText -android.support.design.widget.TextInputEditText seems to work as expected however upon using the android.support.design.button.MaterialButton a runtime InflateException is thrown.

Gradle config:

compileSdkVersion 28

minSdkVersion 21

targetSdkVersion 28

dependencies {
   def lifecycle_version = "1.1.1"
   def nav_version = "1.0.0-alpha05"
   def work_version = "1.0.0-alpha06"
   def supportLibraryVersion = "28.0.0-rc01"

   implementation fileTree(include: ['*.jar'], dir: 'libs')
   api "android.arch.lifecycle:extensions:$lifecycle_version"
   api "android.arch.lifecycle:common-java8:$lifecycle_version"
   api "android.arch.navigation:navigation-fragment:$nav_version"
   api "android.arch.navigation:navigation-ui:$nav_version"
   api "android.arch.work:work-runtime:$work_version"

   api ("com.android.support:appcompat-v7:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })
   api ("com.android.support:design:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })
   api ("com.android.support:cardview-v7:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })
   api ("com.android.support:customtabs:$supportLibraryVersion", {
       exclude group: 'com.android.support', module: 'support-media-compat'
   })

...

StackTrace

    E/XXXXXXApp: Unhandled Exception - Application Crash
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXXXXX/com.XXXXXX.activities.XXXXXXActivity}: android.view.InflateException: Binary XML file line #84: Binary XML file line #84: Error inflating class android.support.design.button.MaterialButton
    at com.XXXXXXActivity.onCreate(LoginActivity.java:107)
...

 Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
                 at android.support.design.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:170)
                 at android.support.design.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:75)
                 at android.support.design.button.MaterialButton.<init>(MaterialButton.java:140)
                 at android.support.design.button.MaterialButton.<init>(MaterialButton.java:133)

EDIT: Added more details to stacktrace and figured out that the problem was to do with the fact that the App Theme needs to be inherited from Theme.MaterialComponents.

Answer

Nabster picture Nabster · Aug 14, 2018

Ensure that the App Theme style is inherited from Theme.MaterialComponents

Example:

<style name="XXXXXAppTheme" parent="@style/Theme.MaterialComponents.Light.DarkActionBar">