问题描述
我试图弄清楚为什么会出现此错误:
I am trying to figure out why I am getting this error:
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时这是上面的类代码:
When trying to run my PreferenceFragmentCompatThis is the above class code:
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);
}
}
这是活动的清单声明,其中显示了片段
This is the activity's manifest declaration that shows the fragment
<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中片段的代码
Code that shows the fragment in the above MainActivity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, HistoryFragment.newInstance());
ft.commit();
我通过MainActivity申请的主题
Theme that I am applying via the MainActivity
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
推荐答案
感谢@Panther建议的解决方案.我要做的就是添加这行<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
我的应用程序主题,而不仅仅是显示我的PreferenceFragment的单个活动的主题:
Thanks to the solution that @Panther suggested. All I had to do is to add this line <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
to my Application Theme and not just the theme of the individual activity that is showing my PreferenceFragment like that:
<!-- 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>
获得现代的材质外观.
这篇关于片段必须在主题onCreate中指定preferenceTheme的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!