错误: IllegalArgumentException: 该组件上的样式要求您的应用程序主题为 Theme.Materials 组件

下面是我的依赖项

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

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

以上代码对我来说工作得很好

但是当我更新 material designdependencies

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

我得到了以下错误:

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
61773 次浏览

material:1.1.0-alpha01有些问题

一个简单的解决方案是更改父主题

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

</style>

我们已经改变了格拉德依赖和样式代码我的情况下工作良好

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

等级依赖

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

从材质组件 舰桥主题扩展您的基础应用程序主题。它扩展了 AppCompat 主题,但添加了所有必要的东西。

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

Theme.MaterialComponentsTheme.MaterialComponents.Light都有 .Bridge 主题:

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

它允许您继续使用库的最新版本,但是可以避免由于迁移到纯材料组件主题而导致的大量 UI 问题

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

| > 1: style.xml中定义以下样式:

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

| > 2: manifest.xml文件中,使用上面的样式:

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

直接给小部件添加主题也可以。例如:

    <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

已经和最小 SDK 21号核对过了。

我有一个错误,当我创建一个芯片的代码,这个问题使应用程序停止工作,所以我解决了创建一个芯片上下文喜欢你的活动名称如下

Chip chip = new Chip(YourNameActivity.this);

这对我来说很有用,我的应用程序使用依赖作为 com.google.android.material:material:1.4.0

如果您遇到这个问题,并且正在以编程方式实例化一个视图:

确保在需要 Context 实例时不使用 getApplicationContext () ,而是在匿名函数中使用 thisHolderActivity.this(用您自己的活动替换 HolderActivity)。

比如说,我和

CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(getApplicationContext());

我应该在哪里使用

CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(MyActivity.this);

我有这个问题时,我正在测试的 编辑文本,这是内部的 TextInputLayout 文本输入输出布局与风格,有家长 Rent = “ Widget.aterialComponent. TextInputLayout.OutlinedBox”

解决方案是在测试中添加主题,如下所示

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

您还可以向坐标布局添加主题,例如

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

我遇到了这个问题,通过改变主题“主题。材料组件。“ LightNoActionBar”和“ Theme”。材料组件。LightNoActionBar.这对我很有用。

如果要在 WindowManager 中添加视图,则必须在布局中设置样式 像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.YOUR_THEME"
android:orientation="vertical">


</LinearLayout>

把分级依赖关系改成这样就行了!

implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'

附注: Android Studio Dolphin | 2021.3.1补丁1