Android dev的新手,从iOS切换到黑暗面,请不要讨厌。
问题:
我将compile 'com.android.support:preference-v7:27.0.0'
添加到了Gradle中。我在styles.xml
中指定了一个主题。但是,我仍然收到如下所示的错误消息。如何解决这个问题?
java.lang.IllegalStateException: Must specify preferenceTheme in theme
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:211)
参考文献:Stackoverflow answer,Medium article
SettingsFragment
public class SettingsFragment extends PreferenceFragmentCompat {
private SwitchPreferenceCompat receiveReminders;
private ListPreference notifyMe;
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
receiveReminders = (SwitchPreferenceCompat) findPreference(R.string.key_receive_reminders);
notifyMe = (ListPreference) findPreference(R.string.key_notify_me);
}
}
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
的preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/settings_category_notifications">
<SwitchPreferenceCompat
android:key="@string/key_receive_reminders"
android:title="@string/settings_receive_reminders"
android:defaultValue="true"/>
<ListPreference
android:key="@string/key_notify_me"
android:title="@string/settings_notify_me"
android:summary="%s"
android:entries="@array/settings_notify_me_titles"
android:entryValues="@array/settings_notify_me_titles"
android:defaultValue="@string/notify_me_2_hours"
android:positiveButtonText="@null"
android:dependency="@string/key_receive_reminders"/>
</PreferenceCategory>
</PreferenceScreen>
答案 0 :(得分:1)
请确保在您的AndroidManifest.xml
文件中设置应用和/或活动的主题,否则将无法找到偏好主题:
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme">
</activity>