Android error - Must specify preferenceTheme in Theme

Asked

Viewed 205 times

1

I created a layout (preferences.xml) in the res/xml directory based on Preferencescreen.

I implemented a class derived from Preferencesfragmentcompat

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

I implemented Activity of preferences

public class PrefsActivity extends AppCompatActivity
{

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

    setContentView(R.layout.prefs_activity);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragContent, new PrefsFragment());
    transaction.commit();
  }

} 

When I run Activity the following error is displayed:

java.lang.Runtimeexception: Unable to start Activity Componentinfo{Prefsactivity}: java.lang.Illegalstateexception: Must specify preferenceTheme in Theme

How do I specify the preferenceTheme?

  • You declared your Activity in Manifest.xml?

  • I declared! It seems to me that when using a layout with <Preferencescreen> I have to declare a different theme, I just don’t know how.

  • What is the minSdkVersion?

  • minSdkVersion 16

1 answer

1

In the archive res/values/styles.xml add

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

If you haven’t changed anything in the file generated by Android Studio will look like this:

<!-- 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="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

If you’re using the v7 Preference Support Library I advise you to replace it with v14 Preference Support Library because in v7 there are "problems" with the preferenceTheme.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.