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

Rol*_*zép 35 android material-components material-components-android

我在我的一个布局文件中有一个com.google.android.material.button.MaterialButton组件,当我使用最新版本的Material Components库时,我收到此错误(com.google.android.material:material:1.0 0.0-素α3):

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

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

Cam*_*ham 51

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

  • 我有同样的问题,你的解决方案工作正常.事实证明,我的AppTheme并没有继承自Theme.MaterialComponents,而是AppCompat.它曾用于1.0.0-alpha1,但不适用于版本3. (4认同)

小智 33

如果您使用任何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>
Run Code Online (Sandbox Code Playgroud)


Aks*_*kar 12

如果您想继续使用旧样式,而只想从'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>
Run Code Online (Sandbox Code Playgroud)


010*_*101 6

我的主题是否从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'
Run Code Online (Sandbox Code Playgroud)

这足以触发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.
Run Code Online (Sandbox Code Playgroud)

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

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

TL; 博士

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

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


Usa*_* US 6

如果您的主题没有扩展 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>
Run Code Online (Sandbox Code Playgroud)

对此:

<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>
Run Code Online (Sandbox Code Playgroud)