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

java.lang.IllegalArgumentException: This component requires that you specify a valid android:textAppearance attribute

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

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

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

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

如果您正在使用任何 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>

我遇到了同样的问题,我更改了 activity 主题,但没有解决问题。我将我的主要应用程序主题从 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>

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

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

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

如果您想继续使用旧样式但只想扩展 '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>

我的主题是否延伸自 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'

这足以触发风味 mainThis component requires that you specify a valid android:textAppearance attribute,而风味 video 的效果很好。每个 main 构建都崩溃了,因为我的应用程序的主题被命名为 AppTheme,并且移动 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 将最疯狂的错误添加到 "stable" 版本的倾向。至少可以通过一些重构很容易地解决。

TL;DR

这是问题所在:https://github.com/tanersener/mobile-ffmpeg/issues/206 再加上当两个合并的清单指定同名的不同主题时,Android 将根据您的 build.gradle.

的内容随机选择一个。

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

即使我的主题扩展 Theme.MaterialComponents 也卡在了这个错误中。我是这样创建 Chips 的:Chip chip = new Chip(getActivity().getBasecontext(), null, R.attr.CustomChipChoice);.

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

希望对您有所帮助。

我先包含了这个依赖

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>

如果您的主题没有扩展 MaterialComponents 主题,并且您想保留 AppCompat 主题,请使用桥主题,这样您就可以使用 material 组件来保留 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>

扩展正确? => 验证您的风格![=2​​3=]

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

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

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

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

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

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).

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