The style on this component requires your app theme to be theme MaterialComponents or a descendant

1. 발생 조건 : Material Object인 Chip을 처음 사용 해 보던 중 발생하였습니다.

아래의 해당 Exception이 발생하며 Crash로 앱이 종료됩니다.

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant). at com.google.android.material.internal.ThemeEnforcement.e(ThemeEnforcement.java:248) at com.google.android.material.internal.ThemeEnforcement.c(ThemeEnforcement.java:222) at com.google.android.material.internal.ThemeEnforcement.b(ThemeEnforcement.java:150) at com.google.android.material.internal.ThemeEnforcement.k(ThemeEnforcement.java:81) at com.google.android.material.chip.ChipDrawable.p1(ChipDrawable.java:334) at com.google.android.material.chip.ChipDrawable.t0(ChipDrawable.java:276) at com.google.android.material.chip.Chip.<init>(Chip.java:193) at com.google.android.material.chip.Chip.<init>(Chip.java:186) at com.google.android.material.chip.Chip.<init>(Chip.java:182)......

2. 발생원인은 Exception에서 알려줍니다.

your app theme to be Theme.MaterialComponents

현재 앱 테마가 MaterialComponents로 설정되어 있지 않기 때문에 발생합니다.

3. 해결방법은 아래와 같이 AppTheme를 MaterialComponents로 변경하면 MaterialComponents들을 사용할 수 있습니다.

The style on this component requires your app theme to be theme MaterialComponents or a descendant
The style on this component requires your app theme to be theme MaterialComponents or a descendant

오늘 bottomsheet를 쓰다가 style을 바꿀 일이 생겨 바꾼 다음 실행을 했는데,

The style on this component requires your app theme to be theme MaterialComponents or a descendant
The style on this component requires your app theme to be theme MaterialComponents or a descendant

이러한 장문의 에러가 발생하였다.

처음 만나보는 에러였기에 혼자 해결하지는 못했고 여러 가지를 열심히 검색해봤다.

처음에는 위에 있는 causeby인 Binary XML file line #29에 대해 찾아봤지만 해답이 나오지 않았다.

그래서 맨 위에 있는 java.lang.RuntimeException: Unable to start activity ComponentInfo~ 이 친구 때문인가하며 

열심히 검색해보며 고쳤지만,

더더욱 삽질만 해댔다.

그러다 아래로 내려보니 또 다른 에러가 있었다.

바로 제목이기도 한 java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant). java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant). 이 친구이다.

이거다 생각해서 검색해보니, 이게 정답이었다.

이 오류는 style에서 apptheme를 내가 Appcompat을 사용하고 있었는데 이 친구를 MaterialComponets로 바꾸란 것이었다.

대충 바꾸기 전에는 

The style on this component requires your app theme to be theme MaterialComponents or a descendant

이렇게 parent가 Them.AppCompat~이었지만

The style on this component requires your app theme to be theme MaterialComponents or a descendant

Theme.MeterialComponets로 바꾸고 나니 잘 실행되었다.

다행히 해결되어서 개인 프로젝트하는데 진행이 막히지 않았다.

All we need is a simple explanation of the problem, which is provided below.

Below are my dependencies

implementation 'com.google.android.material:material:1.0.0' implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

My layout.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:fitsSystemWindows="true"> <com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomBar" style="@style/Widget.MaterialComponents.BottomAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:backgroundTint="@color/colorPrimary" app:fabAlignmentMode="center" app:fabCradleMargin="10dp" app:fabCradleVerticalOffset="4dp" app:navigationIcon="@drawable/ic_drawer" > </com.google.android.material.bottomappbar.BottomAppBar> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_anchor="@id/bottomBar" app:srcCompat="@drawable/ic_apps" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>

The Above code is working fine for me

But when i update the dependencies of material design

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

I’m getting below error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{neel.com.bottomappbar/neel.com.bottomappbar.MainActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.bottomappbar.BottomAppBar at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.bottomappbar.BottomAppBar at android.view.LayoutInflater.inflate(LayoutInflater.java:543) at android.view.LayoutInflater.inflate(LayoutInflater.java:427) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at neel.com.bottomappbar.MainActivity.onCreate(MainActivity.kt:16) at android.app.Activity.performCreate(Activity.java:6358) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)  at android.app.ActivityThread.access$1100(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:157)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)  Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.bottomappbar.BottomAppBar at android.view.LayoutInflater.createView(LayoutInflater.java:649) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) at android.view.LayoutInflater.inflate(LayoutInflater.java:519) at android.view.LayoutInflater.inflate(LayoutInflater.java:427)  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)  at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  at neel.com.bottomappbar.MainActivity.onCreate(MainActivity.kt:16)  at android.app.Activity.performCreate(Activity.java:6358)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)  at android.app.ActivityThread.access$1100(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:157)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)  Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:623) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)  at android.view.LayoutInflater.inflate(LayoutInflater.java:519)  at android.view.LayoutInflater.inflate(LayoutInflater.java:427)  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)  at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  at neel.com.bottomappbar.MainActivity.onCreate(MainActivity.kt:16)  at android.app.Activity.performCreate(Activity.java:6358)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)  at android.app.ActivityThread.access$1100(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:157)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)  Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant). at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240) at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215) at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143) at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:78) at com.google.android.material.bottomappbar.BottomAppBar

Let’s Solve it:

This is a common error many developers questioned us about it. So we write the explanation above. You just have to apply the suggested solution to your code and it will do for you. If you still getting this error after applying this code then comment below we will get back to you with the new method.

Solution 1

There is some issue with material:1.1.0-alpha01

A simple solution is to change the parent theme

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Customize your theme here. --> </style>

Solution 2

Extend your base app Theme from Material Components Bridge theme. It extends AppCompat theme, but adds all necessary stuff.

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

Both Theme.MaterialComponents and Theme.MaterialComponents.Light have .Bridge themes:

Theme.MaterialComponents.Bridge
Theme.MaterialComponents.Light.Bridge
Theme.MaterialComponents.NoActionBar.Bridge
Theme.MaterialComponents.Light.NoActionBar.Bridge
Theme.MaterialComponents.Light.DarkActionBar.Bridge

It allows you to keep using latest version of the library, but avoid a lot of UI issues, caused by migration to pure Material Component theme

https://material.io/develop/android/docs/getting-started/#bridge-themes

Solution 3

Adding theme directly to widget works too. E.g.:

<com.google.android.material.button.MaterialButtonToggleGroup android:id="@+id/toggle_button_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/Theme.MaterialComponents.Light"> <======== THIS

Checked with min. SDK ver. 21.

Solution 4

None of these worked for me. Must do these two things.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Customize your theme --> </style>

and

<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" app:theme="@style/Theme.MaterialComponents"

Solution 5

|> 1: Define the following style in style.xml:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Customize your theme --> </style>

|> 2: In the manifest.xml file, use from above style:

<application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme"> <!-- IMPORTANT (This was my mistake) --> </application>

|> 3: Choose a style for your component:

<com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomBar" style="@style/Widget.MaterialComponents.BottomAppBar" ... > </com.google.android.material.bottomappbar.BottomAppBar>

Solution 6

We have change Gradle dependencies and style code my case working fine

<!-- Base application theme. --> <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> <item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item> <item name="android:textColorSecondary">@color/black_overlay</item> <!--<item name="android:windowBackground">@drawable/nav_menu_background</item>--> </style>

Gradle Dependencies

implementation 'com.google.android.material:material:1.1.0-alpha06'

Solution 7

You can also add theme to CoordinatorLayout, such as

<androidx.coordinatorlayout.widget.CoordinatorLayout android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar.Bridge"

Solution 8

If you’re having this issue and you’re instantiating a view programmatically:

Make sure not to use getApplicationContext() when requiring a Context instance, but instead use this or HolderActivity.this (replace HolderActivity with your own activity) if inside a anonymous function.

For example I was having this issue with

CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(getApplicationContext());

where I should be using

CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(MyActivity.this);

Solution 9

I had this problem when I was testing the TextInputEditText which was inside a TextInputLayout with style that had parent parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox"

And the solution was to add the theme in the tests as follows

val scenario = launchFragmentInContainer<Fragment>(themeResId = R.style.Theme_APP)

Solution 10

I have an error when I create a chip in code, This issue makes the app stop working, so I solved with creating a chip with context like your activity name like below

Chip chip = new Chip(YourNameActivity.this);

It works for me, my app use dependency as com.google.android.material:material:1.4.0

Solution 11

I had this problem and fixed it by changing the theme "Theme.MaterialComponents.Light.NoActionBar" with "Theme.MaterialComponents.Light.NoActionBar.Bridge" and this works for me.

Note: You are free to use these solutions for your personal use. We recommend you apply the first solution to your code because it was tested in our system before posting it on this page.

We are always trying to help the developer community, So we made their work easy. Basically, we collected these data from stackoverflow.com, As it is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0.