android.support.design.button.MaterialButton 抛出 InflateException

android.support.design.button.MaterialButton throws InflateException

我正在尝试使用最新的库版本来关注 The MDC Codelab on codelabs.developers.google.comEditText -android.support.design.widget.TextInputEditText 似乎按预期工作但是在使用 android.support.design.button.MaterialButton 时抛出运行时 InflateException。

Gradle 配置:

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'
   })

...

堆栈跟踪

    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)

编辑: 向堆栈跟踪添加了更多详细信息,并发现问题与 App Theme 需要从 Theme.MaterialComponents.

继承这一事实有关

确保App Theme风格继承自Theme.MaterialComponents

示例:

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

除了添加 Theme.MaterialComponents

1) 如果您只想获得属性而不是默认样式,您还可以添加 Theme.MaterialComponents.Bridge

2) 添加 androidx 和 com.google.android.material 库而不是更改主题。

可以在 (5) The Components of Material Design (Android Dev Summit '18) - YouTube 3:25 分钟

找到更多信息

如果您(还)不能更改整个应用的主题,您也可以只为 this 视图更改:

<com.google.android.material.button.MaterialButton
    android:id="@+id/fooButon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:fontFamily="sans-serif"
    android:padding="8dp"
==> android:theme="@style/Theme.MaterialComponents.Light"
    app:backgroundTint="@color/base_white" />

使用从 1.0.0 到最新的任何 Material 组件版本时也可能会出现错误,这是由于外部库中指定的主题导致主题命名冲突引起的,例如 Mobile FFmpeg. In this case you need to change the name of your own theme. For further information I explain this in more detail in an answer to this similar question:

基本上,如果您遇到此问题,但知道您正在正确使用 MaterialComponents 主题,请使用 "Find in Path" 在 Android Studio 中搜索您的应用主题 -> "Scope" -> "All Places"。例如,在构建和 运行 你的应用程序之后,如果你的主题名称是 AppTheme,搜索 "AppTheme" 包括引号以查看是否有缓存的 XML 文件指定完全相同的名字。

如果您不想更改应用的整个主题并回答否。 2 使您的按钮消失,然后我建议将其添加到您的 MaterialButton

android:theme="@style/Theme.MaterialComponents.NoActionBar"