错误:IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.MaterialComponents
Posted
技术标签:
【中文标题】错误:IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.MaterialComponents【英文标题】:Error : IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents 【发布时间】:2019-04-27 19:16:34 【问题描述】:以下是我的依赖项
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
我的布局.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_
android:layout_
android:background="#ffffff"
android:fitsSystemWindows="true">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
style="@style/Widget.MaterialComponents.BottomAppBar"
android:layout_
android:layout_
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_
android:layout_
app:layout_anchor="@id/bottomBar"
app:srcCompat="@drawable/ic_apps" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
上面的代码对我来说很好用
但是当我更新material design
的dependencies
时
implementation 'com.google.android.material:material:1.1.0-alpha01'
我遇到以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfoneel.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
【问题讨论】:
【参考方案1】:material:1.1.0-alpha01
存在一些问题
一个简单的解决方案是更改父主题
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
【讨论】:
com.google.android.material:material:1.1.0
中仍然存在问题。
我在com.google.android.material:material:1.1.0
中遇到了同样的问题,但通过更改父主题解决了这个问题。
悟空你帮助了我,点赞是理所当然的。
这是可行的,但可能会稍微改变您的设计。
这改变了组件的视图,所以设计......这就是Android Studio所说的......这不是一个好的解决方案【参考方案2】:
我们已经更改了 Gradle 依赖项和样式代码,我的案例工作正常
<!-- 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 依赖项
implementation 'com.google.android.material:material:1.1.0-alpha06'
【讨论】:
【参考方案3】:从 Material Components Bridge 主题扩展您的基础应用主题。它扩展了 AppCompat 主题,但添加了所有必要的东西。
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->
</style>
Theme.MaterialComponents
和Theme.MaterialComponents.Light
都有.Bridge
主题:
Theme.MaterialComponents.Bridge
Theme.MaterialComponents.Light.Bridge
Theme.MaterialComponents.NoActionBar.Bridge
Theme.MaterialComponents.Light.NoActionBar.Bridge
Theme.MaterialComponents.Light.DarkActionBar.Bridge
它允许您继续使用最新版本的库,但避免由于迁移到纯 Material Component 主题而导致的大量 UI 问题
https://material.io/develop/android/docs/getting-started/#bridge-themes
【讨论】:
【参考方案4】:|> 1:在style.xml
中定义如下样式:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme -->
</style>
|> 2:在manifest.xml
文件中,使用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:为您的组件选择样式:
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomBar"
style="@style/Widget.MaterialComponents.BottomAppBar"
...
>
</com.google.android.material.bottomappbar.BottomAppBar>
【讨论】:
非常感谢。我相信这应该是最好的答案,因为您是唯一一个提到应该在 style.xml 文件中进行更改的人。 (位于 values 文件夹中)【参考方案5】:这些都不适合我。必须做到这两点。
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme -->
</style>
和
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
app:theme="@style/Theme.MaterialComponents"
【讨论】:
非常感谢【参考方案6】:将主题直接添加到小部件也可以。例如:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_button_group"
android:layout_
android:layout_
android:theme="@style/Theme.MaterialComponents.Light"> <======== THIS
用 min 检查。 SDK版本。 21.
【讨论】:
我怎样才能为程序创建的芯片做到这一点? 这比更改整个应用主题要好【参考方案7】:我在代码中创建芯片时出现错误,此问题使应用程序停止工作,因此我通过创建一个具有如下活动名称的上下文的芯片来解决
芯片芯片 = 新芯片(YourNameActivity.this);
它适用于我,我的应用使用依赖项为 com.google.android.material:material:1.4.0
【讨论】:
请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。【参考方案8】:如果您遇到此问题并且正在以编程方式实例化视图:
确保在需要 Context 实例时不要使用 getApplicationContext(),而如果在匿名函数中使用 this
或 HolderActivity.this
(将 HolderActivity 替换为您自己的活动)。
例如我遇到了这个问题
CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(getApplicationContext());
我应该在哪里使用
CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(MyActivity.this);
【讨论】:
以上是关于错误:IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.MaterialComponents的主要内容,如果未能解决你的问题,请参考以下文章
SpringData : xxx_xxx is not mapped
远程服务器返回错误: 404错误远程服务器返回错误:500错误 HttpWebResponse远程服务器返回错误:(404500) 错误。
Pig 安装错误:错误 pig.Main:错误 2998:未处理的内部错误