片段必须在主题onCreate中指定preferenceTheme

Geo*_*iev 15 android android-fragments

我试图弄清楚为什么我收到此错误:

java.lang.IllegalStateException: Must specify preferenceTheme in theme
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:210)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2177)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager
Run Code Online (Sandbox Code Playgroud)

尝试运行我的PreferenceFragmentCompat时这是上面的类代码:

public class SettingsFragment extends PreferenceFragmentCompat {

public SettingsFragment() {
    // Required empty public constructor
}

public static SettingsFragment newInstance() {
    SettingsFragment fragment = new SettingsFragment();
    return fragment;
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.settings_preferences, rootKey);
}

}
Run Code Online (Sandbox Code Playgroud)

这是显示片段的活动的清单声明

<activity
        android:name=".active_minutes_screen.view.ActiveMinutesActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

显示上述MainActivity中的片段的代码

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, HistoryFragment.newInstance());
ft.commit();
Run Code Online (Sandbox Code Playgroud)

我通过MainActivity申请的主题

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
Run Code Online (Sandbox Code Playgroud)

Geo*_*iev 58

感谢@Panther建议的解决方案.我所要做的就是将这一行添加<item name="preferenceTheme">@style/PreferenceThemeOverlay</item> 到我的应用程序主题中,而不仅仅是显示我的PreferenceFragment的单个活动的主题:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

或者你可以使用

<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>

实现现代材料外观.