1

New to android dev, switched over to the dark side from iOS, no hate please.

Question: I added compile 'com.android.support:preference-v7:27.0.0' to my Gradle. I specified a theme in styles.xml. However, I still get the error message shown below. How to fix this?

java.lang.IllegalStateException: Must specify preferenceTheme in theme

at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:211)

References: Stackoverflow answer, Medium article

SettingsFragment

public class SettingsFragment extends PreferenceFragmentCompat {
    private SwitchPreferenceCompat receiveReminders;
    private ListPreference notifyMe;

    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        receiveReminders = (SwitchPreferenceCompat) findPreference(R.string.key_receive_reminders);
        notifyMe = (ListPreference) findPreference(R.string.key_notify_me);
    }
}

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

preferences.xml

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="@string/settings_category_notifications">
    <SwitchPreferenceCompat
        android:key="@string/key_receive_reminders"
        android:title="@string/settings_receive_reminders"
        android:defaultValue="true"/>
    <ListPreference
        android:key="@string/key_notify_me"
        android:title="@string/settings_notify_me"
        android:summary="%s"
        android:entries="@array/settings_notify_me_titles"
        android:entryValues="@array/settings_notify_me_titles"
        android:defaultValue="@string/notify_me_2_hours"
        android:positiveButtonText="@null"
        android:dependency="@string/key_receive_reminders"/>
</PreferenceCategory>
</PreferenceScreen>
Alex
  • 2,369
  • 3
  • 13
  • 23

1 Answers1

1

Make sure that in your AndroidManifest.xml file you are setting the theme on your app and/or activity or else the preferenceTheme will not be found:

<activity
    android:name=".MainActivity"
    android:theme="@style/AppTheme">
</activity>
dustinrwh
  • 888
  • 1
  • 14
  • 16