我想弄明白为什么我会收到这个错误:
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
尝试运行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);
}
}
这是显示片段
的活动的清单声明<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>
显示上述MainActivity中片段的代码
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, HistoryFragment.newInstance());
ft.commit();
我通过MainActivity申请的主题
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
答案 0 :(得分:57)
感谢@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>
或者你可以使用
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
实现现代材料外观。