0

编辑:在进一步检查中,似乎只有 NightMode 会导致此崩溃,无论我是自己明确设置它,还是让 Android 的系统范围设置来处理它!

我有一个使用 AppCompat 主题支持日/夜模式的应用程序。在启动时,它会应用存储设置中的白天/夜晚样式(用户也可以随时从应用程序内更新设置)。

为此:

int mode = GlobalAppSettings.getStoredSettings().nightModeEnabled() ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO;
AppCompatDelegate.setDefaultNightMode(mode);

我最近决定要使用 Material 中的 Chip 小部件来实现标签,但是当我的应用程序尝试为其中包含 Chip 的视图充气时,我得到了错误:

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

我已经更新了我的风格主题,以从 MaterialComponents 派生,但似乎没有任何帮助。它仍然崩溃了。然后我注释掉了:

AppCompatDelegate.setDefaultNightMode(mode);

虽然该应用程序似乎停留在白天模式,但芯片现在已正确充气。强制日/夜主题的 AppCompatDelegate 方法是否与 MaterialComponents.DayNight 主题不兼容?

有没有其他人遇到过这个问题?

我的风格:

    <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
            <!-- 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="android:windowContentTransitions">true</item>
            <item name="android:windowDisablePreview">true</item>
        </style>
    
        <style name="SplashTheme" parent="AppTheme">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowBackground">@drawable/launch_image</item>
        </style>
    
        <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />
        <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light" />
    
        <style name="EditText.Search" parent="Widget.AppCompat.EditText">
            <item name="colorControlNormal">@color/black_overlay</item>
            <item name="colorControlActivated">@color/black_overlay</item>
        </style>
    
        <style name="GameyeToolbarTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
            <item name="android:textColor">@color/toolbar_title_color</item>
            <item name="android:textSize">@dimen/toolbar_title_text_size</item>
        </style>
4

1 回答 1

0

我最终只是直接在芯片上设置了主题

android:theme="@style/Theme.MaterialComponents"

鉴于我的它们源自 MaterialComponents,这似乎没有必要。可能是材质的bug?

我打开了一张关于材料的票,看看他们是否有任何想法。 https://github.com/material-components/material-components-android/issues/1872

于 2020-11-15T19:04:55.033 回答