0

I am trying to add a settings activity to my app as described in https://developer.android.com/guide/topics/ui/settings but after adding initial implementation, when my settings activity is loaded I get a "You need to use a Theme.AppCompat theme (or descendant) with this activity." exception.

Fragment Must specify preferenceTheme in theme onCreate You need to use a Theme.AppCompat theme (or descendant) with this activity

According to https://developer.android.com/jetpack/androidx/releases/archive/androidx this issue should not occur as of 1.0.0-rc01 androidX release and yet I am experiencing it.

as workarounds i tried to hardcode the theme for the settings activity in the activity's xml layout file, and also tried to add a preferenceTheme item to my app's styles.xml. none worked.

/* settings activity + fragment */

public class SettingsPrefActivity extends AppCompatActivity {
    private static final String TAG = SettingsPrefActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // load settings fragment

        getSupportFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, new MainPreferenceFragment())
                .commit();
    }

    public static class MainPreferenceFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.pref_main, rootKey);
        }
}

My 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>
    </style>

</resources>

to clarify I also tried specifically adding the following which didn't resolve the issue.

<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
yo1la
  • 91
  • 1
  • 11
  • 2
    your problem is with your styles.xml file, this link you posted should be your answer https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity – a_local_nobody Jul 31 '19 at 08:31
  • thanks. problem was with the manifest not the styles.xml. but your direction of that thread helped me figure that out. – yo1la Jul 31 '19 at 11:35
  • no problem, happy to help :) – a_local_nobody Jul 31 '19 at 11:35

1 Answers1

1

as @a_local_nobody suggested i went back to that linked thread and read it more thoroughly. turns out i was overriding the activity's theme in the app's manifest :( cleared that and the activity renders properly. Thanks!

yo1la
  • 91
  • 1
  • 11