我正在 viewpager 中创建 PreferenceFragment 并收到错误

java.lang.IllegalStateException: Must specify preferenceTheme in theme 

我提到了PreferenceFragmentCompat requires preferenceTheme to be set但没能解决。我添加了 styles.xml,如下所示。

<resources> 
 
    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.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="android:preferenceStyle">@style/PreferenceThemeOverlay</item> 
    </style> 
 
    <style name="SettingsTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item> 
    </style> 
</resources> 

下面是 list 代码

  <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme" > 
        <activity android:name=".MainActivity" 
            android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 

以下是首选项屏幕的代码

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="screen"> 
 
    <SwitchPreference 
        android:key="sc" 
        android:summary="Hi" 
        android:title="Hello" /> 
 
</PreferenceScreen> 

你能给我建议吗,我第一次使用主题,并对它感到困惑。仍然遇到同样的错误。

请您参考如下方法:

manifest 中的 Activity 标记中尝试此操作:

<activity android:name=".MainActivity" 
        android:theme="@style/AppTheme"> 
        <intent-filter> 
            <action android:name="android.intent.action.MAIN" /> 
 
            <category android:name="android.intent.category.LAUNCHER" /> 
        </intent-filter> 
    </activity> 

您加载首选项 fragment activity没有设置主题。

同时在您的Apptheme中进行设置

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item> 

而不是:

<item name="android:preferenceStyle">@style/PreferenceThemeOverlay</item> 

将此行添加到您的Apptheme

<item name="windowActionBar">false</item> 
<item name="windowNoTitle">true</item> 


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!