Skip to content
Advertisement

AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant)

iOS developer here thrown to the wolves of an Android project. I am getting a few error codes that all say something similar to ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant). as soon as the following code snippet runs:

view = inflater.inflate(R.layout.f_basic_dashboard_screen, null, false)

My inflater is instantiated as follows:

val inflater: LayoutInflater = LayoutInflater.from(SqueaksApplication.getApplicationInstance())

SqueaksApplication.getApplicationInstance() just returns an instance of public class SqueaksApplication extends Application

Almost every answer I have found to this question points to the project’s manifest as the issue and needing to provide a theme for the project. My project’s manifest looks like the following:

<application
  android:name=".SqueaksApplication"
  android:allowBackup="false"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsRtl="true"
  android:theme="@style/AppTheme"
  android:usesCleartextTraffic="true"
  tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"
  tools:replace="android:icon,android:allowBackup">

As you can see my manifest already includes the line android:theme="@style/AppTheme"

My project uses the layout inflator in many different places, but this is the only one I have come across that is throwing errors. Would appreciate any insight, or solutions you may have, thanks!

Edit: As requested this is my styles.xml:

<style name="MainActivityTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:actionMenuTextColor">#FFF</item>
    <item name="actionBarStyle">@style/MainActivityTheme.Toolbar</item>
    <item name="actionBarTheme">@style/MainActivityTheme.ToolbarOverlay</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="toolbarStyle">@style/MainActivityTheme.Toolbar</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorPrimary">@color/text_primary</item>
    <item name="android:textColorSecondary">@color/text_secondary</item>
    <item name="android:windowBackground">@color/background</item>
    <item name="actionOverflowButtonStyle">@style/MainActivityTheme.ActionOverflow</item>
    <item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>

<style name="AlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColorPrimary">#DE000000</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="MainActivityTheme.ActionOverflow" parent="Base.Widget.AppCompat.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_settings</item>
</style>

<style name="MainActivityTheme.Toolbar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="background">@color/colorPrimary</item>
    <item name="elevation">4dp</item>
    <item name="titleTextAppearance">@style/MainActivityTheme.ToolbarTextAppearance</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

<style name="MainActivityTheme.ToolbarOverlay" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

<style name="MainActivityTheme.ToolbarTextAppearance" parent="TextAppearance.AppCompat.Title">
    <item name="android:textColor">#81be41</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:actionMenuTextColor">#FFF</item>
    <item name="actionBarStyle">@style/Toolbar</item>
    <item name="actionBarTheme">@style/ToolbarOverlay</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="toolbarStyle">@style/Toolbar</item>
    <item name="android:textColorPrimary">@color/text_primary</item>
    <item name="android:textColorSecondary">@color/text_secondary</item>
    <item name="android:windowBackground">@color/background</item>
    <item name="actionOverflowButtonStyle">@style/ActionOverflow</item>
    <item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>

<style name="AppTheme.Dark" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:windowBackground">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="ActionOverflow" parent="Base.Widget.AppCompat.ActionButton.Overflow">
    <item name="android:src">@drawable/ic_dots_vertical_white</item>
</style>

<style name="Toolbar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="background">@color/colorPrimary</item>
    <item name="elevation">4dp</item>
    <item name="titleTextAppearance">@style/ToolbarTextAppearance</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

<style name="ToolbarOverlay" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

<style name="ToolbarTextAppearance" parent="TextAppearance.AppCompat.Title">
    <item name="android:textColor">#81be41</item>
    <item name="android:textSize">20sp</item>
</style>

<style name="Splash" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

<style name="squeak_item_popup_menu_animation">
    <item name="android:windowEnterAnimation">@anim/squeak_item_popup_menu_animation_enter</item>
    <item name="android:windowExitAnimation">@anim/squeak_item_popup_menu_animation_exit</item>
</style>

<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

Advertisement

Answer

Your issue is the context used: val inflater: LayoutInflater = LayoutInflater.from(SqueaksApplication.getApplicationInstance()).

The ApplicationContext doesn’t have your app theme.
You need to pass the Activity, not an Application Context.

Advertisement