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

百般思念 提交于 2019-11-26 23:18:48

问题


I have a com.google.android.material.button.MaterialButton component in one of my layout file and I get this error when I am using the latest version of the Material Components library (com.google.android.material:material:1.0.0-alpha3):

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

It wasn't present in 1.0.0-alpha1. Is this a bug in the library or should I just specify a textAppearance attribute from now?


回答1:


Does your theme extend from Theme.MaterialComponents? More info about how to ensure all the components will work correctly can be found at https://material.io/develop/android/docs/getting-started/




回答2:


If you are using any of the MaterialComponent, then your theme must extend from it theme "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:


if u want to keep using your old styles but only want to extend from 'Theme.MaterialComponents' then you can use '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:


Check if your AppTheme inherits from MaterialComponents as specified here.

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

Remember to check all variants of styles.xml file. This was actually the issue I had.




回答5:


Does my theme extend from Theme.MaterialComponents? Oh yes indeed, and has been since I started using any of the new Material UI stuff. If all these answers here are as unhelpful to you as they were to me, get ready: The This component requires that you specify a valid android:textAppearance attribute error can be related to an external library specifying android:theme with the same name as the theme you are using, and Android randomly deciding which one to use depending on your build.gradle. In my case the culprit was inside Mobile FFmpeg.

I started encountering this error after working for a week while the build variant was set to a different product flavor and then switching back the original one. What changed meanwhile? After thorough investigation I found the build broke after I split implementation 'com.arthenica:mobile-ffmpeg-min:4.2.2.LTS' in two, for each product flavor where I actually use it like this:

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

This was enough to trigger This component requires that you specify a valid android:textAppearance attribute for flavor main, while working fine for flavor video. Every main build crashed because my app's theme is named AppTheme and the Mobile FFmpeg manifest also specifies android:theme="@style/AppTheme" (which affects all versions up to 4.2.2). So I renamed my theme, but that resulted in a build error very similar to the one here: 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.

After adding said tools:replace="android:theme", everything worked again, and the original MaterialComponents error was gone.

But why is it OK for one flavor and not OK for the other? Absolutely no idea. Credit goes to Google's tendency to add the craziest bugs to "stable" releases. At least it's possible to solve very easily with some refactoring.

TL;DR

THIS is the issue: https://github.com/tanersener/mobile-ffmpeg/issues/206 Together with the fact that when two merged manifest specify different themes with the same name, Android will choose one randomly depending on the content of your build.gradle.

Solution: Add a tools:replace="android:theme" to your manifest's <application> tag and change the name of your theme.




回答6:


Got stucked in this error even if my theme extends Theme.MaterialComponents. I was creating Chips like this :Chip chip = new Chip(getActivity().getBasecontext(), null, R.attr.CustomChipChoice);.

The solution is to change it to Chip chip = new Chip(getActivity(), null, R.attr.CustomChipChoice);.

Hope it helps.




回答7:


I had the same problem, I changed my activity theme but it didnt resolved the issue. The i changed my main app theme from AppCompact to 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>



回答8:


I have included this dependence first

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

Than I have set my project style as bellow

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


来源:https://stackoverflow.com/questions/50821456/java-lang-illegalargumentexception-this-component-requires-that-you-specify-a-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!