1

I am creating PreferenceFragment in viewpager and getting error as

java.lang.IllegalStateException: Must specify preferenceTheme in theme

I referred PreferenceFragmentCompat requires preferenceTheme to be set but did not able to resolve. I added my styles.xml as below.

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

and below is code for manifest

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

Below is code for pref screen

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

Can u advice, I m using theme first time and getting confused over it. Still getting same error.

Community
  • 1
  • 1
Panache
  • 1,701
  • 3
  • 19
  • 33

1 Answers1

1

try this in your activity tag in manifest:

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

your activity that was loading the preference fragment didn't have the theme set.

Also set this in your Apptheme

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

instead of:

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

add this lines in your Apptheme

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • New error: java.lang.RuntimeException: Unable to start activity ComponentInfo{jss.fragments/jss.fragments.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. – Panache Jan 15 '17 at 16:04