【问题标题】:java.lang.IllegalArgumentException: This component requires that you specify a valid android:textAppearance attributejava.lang.IllegalArgumentException:此组件要求您指定有效的 android:textAppearance 属性
【发布时间】:2018-11-22 02:30:33
【问题描述】:

我的一个布局文件中有一个 com.google.android.material.button.MaterialButton 组件,当我使用最新版本的材料组件库 (com.google.android.material:材料:1.0.0-alpha3):

java.lang.IllegalArgumentException:此组件要求您指定有效的 android:textAppearance 属性。

它在 1.0.0-alpha1 中不存在。这是库中的错误还是我应该从现在开始指定一个 textAppearance 属性?

【问题讨论】:

    标签: android material-components material-components-android


    【解决方案1】:

    您的主题是否扩展自 Theme.MaterialComponents?有关如何确保所有组件正常工作的更多信息,请访问https://material.io/develop/android/docs/getting-started/

    【讨论】:

    • 我遇到了同样的问题,您的解决方案运行良好。原来我的AppTheme不是继承自Theme.MaterialComponents,而是AppCompat。它曾经适用于 1.0.0-alpha1,但不适用于版本 3。
    • 谢谢!然后我想我会简单地指定一个 textAppearance 属性,因为我想坚持设备默认主题。
    • @RolandSzép 即使您设置了正确的应用主题,它也应该使用设备默认设置。如果您打算使用 Material Design Library 中的任何其他组件,您应该设置正确的主题。它们都引用主题中设置的属性来设置正确的文本外观。如果您想设置不同的文本外观,您可能应该更新主题中的文本外观属性。更多信息:material.io/develop/android/theming/typography
    • @Cameron Ketcham 现在我明白了!我的问题是,当我将 Theme.MaterialComponents 设置为我的应用程序主题(而不是 android:Theme.DeviceDefault.Light)时,一切都变成了默认的“googlish”外观。 (例如,我的应用程序在三星手机上的设置活动不是默认的三星设置外观)但现在我意识到我只需要在我的组件上设置 Theme.MaterialComponents 主题,这些组件来自 Material Design Library。感谢您提供有关字体的链接,它真的很有帮助。
    • 对我来说应用主题是正确的,但活动主题不是从材料主题继承的。请记住活动主题也应该扩展 Material 主题。
    【解决方案2】:

    如果您使用任何 MaterialComponent,那么您的主题必须从以下主题扩展 - Theme.MaterialComponents.Light.DarkActionBar

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    【讨论】:

    【解决方案3】:

    如果您想继续使用旧样式但只想从“Theme.MaterialComponents”扩展,那么您可以使用“Bridge”。

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorAccent</item>
            <item name="colorAccent">@color/colorPrimaryDark</item>
    </style>
    

    【讨论】:

      【解决方案4】:

      如果您的主题没有扩展 MaterialComponents 主题,并且您希望保留 AppCompat 主题,请使用桥接主题,这将允许您使用保留 AppCompat 主题的材质组件。

      从以下位置更改您现有的主题:

       <style name="AppTheme" parent="Theme.AppCompat.Light">
          <!-- Customize your theme here. -->
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      </style>
      

      到这里:

      <style name="AppTheme" parent="Theme.MaterialComponents.Light.Bridge">
          <!-- Customize your theme here. -->
          <item name="colorPrimary">@color/colorPrimary</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
          <item name="colorAccent">@color/colorAccent</item>
      </style>
      

      【讨论】:

      • 谢谢,成功了!
      【解决方案5】:

      我的主题是从Theme.MaterialComponents 扩展而来的吗?哦,是的,确实,自从我开始使用任何新的 Material UI 东西以来就一直如此。 如果这里的所有这些答案对您和对我一样无济于事,请做好准备:This component requires that you specify a valid android:textAppearance attribute 错误可能与 指定android:theme 与您使用的主题同名的外部库有关,并且Android 根据你的 build.gradle 随机决定使用哪一个。就我而言,罪魁祸首在Mobile FFmpeg 内部。

      我在工作一周后开始遇到此错误,而构建变体被设置为不同的产品风格,然后切换回原来的风格。同时发生了什么变化?经过彻底调查后,我发现在我将implementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS' 分成两部分后构建中断,对于我实际使用它的每种产品风味,如下所示:

      videoImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'
      mainImplementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS'
      

      这足以触发This component requires that you specify a valid android:textAppearance attribute 用于风味main,同时可以正常用于风味video。每个main 构建都崩溃了,因为我的应用程序的主题名为AppTheme,并且Mobile FFmpeg 清单还指定了android:theme="@style/AppTheme"(这会影响到4.2.2 的所有版本)。所以我重命名了我的主题,但这导致了一个非常类似于这里的构建错误: https://github.com/tanersener/mobile-ffmpeg/issues/206

          Attribute application@theme value=(@style/ScryptedTheme) from AndroidManifest.xml:37:9-45
          is also present at [com.arthenica:mobile-ffmpeg-https:4.2.LTS] AndroidManifest.xml:17:9-40 value=(@style/AppTheme).
          Suggestion: add 'tools:replace="android:theme"' to <application> element at AndroidManifest.xml:31:5-95:19 to override.
      

      添加说tools:replace="android:theme"后,一切又恢复了,原来的MaterialComponents错误消失了。

      但是为什么一种口味可以,而另一种口味不行呢?完全不知道。这要归功于 Google 倾向于将最疯狂的错误添加到“稳定”版本中。至少可以通过一些重构很容易地解决。

      TL;DR

      这是问题所在:https://github.com/tanersener/mobile-ffmpeg/issues/206 再加上当两个合并的 manifest 指定同名的不同主题时,Android 会根据你的 build.gradle 的内容随机选择一个。

      解决方案:将 tools:replace="android:theme" 添加到清单的 &lt;application&gt; 标记并更改主题名称

      【讨论】:

      • 感谢您的输入..在这个错误上已经抓了很多天了!!!但是当我们赋予这个属性时会发生什么呢?选择了哪个主题,我们的还是图书馆的?
      • tools:replace 会选择你的,但出于某种原因,只有当主题名称与库的名称不同时
      • 我是一个初学者,如果你不介意问,我应该读什么,我将自己分析/解释这些东西..
      • 对不起,我不明白你的问题,你能改写吗?
      • 如果这个命令将库的主题替换为我们的清单主题,库的行为不会改变吗?
      【解决方案6】:

      检查您的 AppTheme 是否按照指定的 here 继承自 MaterialComponents。

      <style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
          <!-- ... -->
      </style>
      

      记得检查styles.xml 文件的所有变体。这实际上是我遇到的问题。

      【讨论】:

        【解决方案7】:

        即使我的主题扩展 Theme.MaterialComponents,也会陷入此错误。我正在创建这样的芯片:Chip chip = new Chip(getActivity().getBasecontext(), null, R.attr.CustomChipChoice);

        解决办法是改成Chip chip = new Chip(getActivity(), null, R.attr.CustomChipChoice);

        希望对你有帮助。

        【讨论】:

        • 嗨@Diridou,这里的CustomChipChoice 是什么?你能给我看看吗?
        【解决方案8】:

        我已经先包含了这个依赖

        implementation 'com.google.android.material:material:1.2.0-alpha01'
        

        比我设置的项目风格如下

         <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
        

        【讨论】:

          【解决方案9】:

          我有同样的问题,我改变了我的活动主题,但它没有解决问题。我将我的主要应用主题从 AppCompact 更改为 Theme.MaterialComponents

          <application
              android:allowBackup="true"
              android:fullBackupContent="true"
              android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
              android:supportsRtl="true"
              android:theme="@style/AppTheme2">
          <activity
                  android:name=".MainActivity"
                  android:label="@string/app_name"/>
          </application>
          

          【讨论】:

            【解决方案10】:

            正确扩展? => 验证你的风格!

            就我而言,我犯了一个愚蠢的自动完成错误:

            <item name="textInputStyle">@style/Widget.MaterialComponents.Button.OutlinedButton</item>
            

            我真正的意思是TextInputLayout.OutlinedBox 而不是Button.OutlinedButton

            <item name="textInputStyle">@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox</item>
            

            不幸的是,这个错字给出了同样的误导性错误(尽管正确扩展了主题):

            引起:java.lang.IllegalArgumentException:这个组件需要 您指定了一个有效的 TextAppearance 属性。更新您的应用 从 Theme.MaterialComponents(或后代)继承的主题。

            ...分享,因为我花了很长时间才发现错误!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-08-09
              • 2016-11-25
              • 1970-01-01
              • 2017-01-11
              • 2017-05-06
              • 2019-06-23
              • 2023-03-18
              • 2014-10-25
              相关资源
              最近更新 更多