66

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

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

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

4

10 回答 10

79

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

于 2018-06-12T18:28:28.050 回答
60

如果您使用任何 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>
于 2018-10-30T11:23:56.830 回答
46

如果您想继续使用旧样式但只想从“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>
于 2019-08-18T07:51:06.913 回答
9

如果您的主题没有扩展 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>
于 2020-04-15T08:17:23.017 回答
6

我的主题是否延伸Theme.MaterialComponents?哦,是的,而且自从我开始使用任何新的 Material UI 东西以来就一直如此。如果这里的所有这些答案对您和对我一样无济于事,请做好准备:该This component requires that you specify a valid android:textAppearance attribute错误可能与指定与您正在使用的主题同名的外部库有关,Android 根据随机决定使用哪一个android:theme你的 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.

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

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

TL;博士

这就是问题所在:https ://github.com/tanersener/mobile-ffmpeg/issues/206 再加上当两个合并的清单指定具有相同名称的不同主题时,Android 将根据您的内容随机选择一个build.gradle.

解决方案:将 a 添加tools:replace="android:theme"到清单的<application>标签并更改主题的名称

于 2019-08-20T01:53:41.887 回答
4

检查您的 AppTheme 是否从此处指定的 MaterialComponents 继承。

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

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

于 2019-07-11T09:23:37.880 回答
2

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

解决方案是将其更改为Chip chip = new Chip(getActivity(), null, R.attr.CustomChipChoice);.

希望能帮助到你。

于 2019-08-26T09:44:37.463 回答
2

我首先包含了这个依赖

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>
于 2019-11-04T13:04:42.163 回答
1

我有同样的问题,我改变了我的活动主题,但它没有解决问题。我将我的主要应用程序主题从 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>
于 2018-12-10T10:06:27.703 回答
1

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

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

<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(或后代)继承。

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

于 2021-03-17T04:27:17.313 回答