为什么我的Android偏好设置没有更新?

时间:2014-05-02 14:39:40

标签: java android xml sharedpreferences

我在xml文件中设置了一些首选项,并使用标准结构(请参阅下面的代码)使它们可用且可更新,并且一切正常。由于这些是出于测试目的,我现在想用我的实际应用程序首选项(包括完全删除一些)来替换它们,但无论我做什么,它似乎只能识别我设置的原始应用程序。我甚至完全用我的新文件替换了xml文件 - 它仍然只找到旧的首选项。我还清除了代码中显示的条目(commit()后的映射大小为0)。此外,它似乎不允许我从' preferences.xml'更改xml文件名。代码显示了我对preferences_new.xml的新名称,但这在Eclipse中引发了错误。

在调用活动onCreate:

        // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new PrefsFragment()).commit();

在PrefsFragment中:

        PreferenceManager pm = this.getPreferenceManager();
    SharedPreferences sp = pm.getSharedPreferences();
    Editor e = sp.edit();
    e.clear();
    e.commit();
    Map <String, ?> map = sp.getAll();
    int size = map.size();
    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences_new);

旧XML文件:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/inline_preferences" >
    <CheckBoxPreference
        android:key="checkbox_preference"
        android:summary="@string/summary_checkbox_preference"
        android:title="@string/title_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/dialog_based_preferences" >
    <EditTextPreference
        android:dialogTitle="@string/dialog_title_edittext_preference"
        android:key="edittext_preference"
        android:summary="@string/summary_edittext_preference"
        android:title="@string/title_edittext_preference" />

    <ListPreference
        android:dialogTitle="@string/dialog_title_list_preference"
        android:entries="@array/entries_list_preference"
        android:entryValues="@array/entryvalues_list_preference"
        android:key="list_preference"
        android:summary="@string/summary_list_preference"
        android:title="@string/title_list_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/launch_preferences" >

    <!--
         This PreferenceScreen tag serves as a screen break (similar to page break
         in word processing). Like for other preference types, we assign a key
         here so it is able to save and restore its instance state.
    -->
    <PreferenceScreen
        android:key="screen_preference"
        android:summary="@string/summary_screen_preference"
        android:title="@string/title_screen_preference" >

        <!-- You can place more preferences here that will be shown on the next screen. -->

        <CheckBoxPreference
            android:key="next_screen_checkbox_preference"
            android:summary="@string/summary_next_screen_toggle_preference"
            android:title="@string/title_next_screen_toggle_preference" />
    </PreferenceScreen>
    <PreferenceScreen
        android:summary="@string/summary_intent_preference"
        android:title="@string/title_intent_preference" >
        <intent
            android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />
    </PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="@string/preference_attributes" >
    <CheckBoxPreference
        android:key="parent_checkbox_preference"
        android:summary="@string/summary_parent_preference"
        android:title="@string/title_parent_preference" />

    <!-- The visual style of a child is defined by this styled theme attribute. -->
    <CheckBoxPreference
        android:dependency="parent_checkbox_preference"
        android:key="child_checkbox_preference"
        android:layout="?android:attr/preferenceLayoutChild"
        android:summary="@string/summary_child_preference"
        android:title="@string/title_child_preference" />
</PreferenceCategory>

新的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="@string/inline_preferences" >
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="checkbox_preference"
        android:summary="@string/summary_checkbox_preference"
        android:title="@string/title_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/dialog_based_preferences" 
                android:key="edittext_category" >
    <EditTextPreference
        android:id="@+id/my_phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:defaultValue=""
        android:dialogTitle="@string/dialog_title_edittext_preference"
        android:inputType="phone"
        android:key="edittext_preference"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:summary="@string/summary_edittext_preference"
        android:title="@string/title_edittext_preference" />
</PreferenceCategory>

我几乎要把头发撕掉了。阶段,所以任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我现在认为我无法替换首选项文件的原因是R.java由于某些难以解决的原因而未被重新生成。我放弃了调查(跟here之后)并回到我之前的工作版本并重新应用更改,直到优先工作才使我的应用程序恢复工作(显示保留代码版本的重要性)。这对我来说是一个更快的选择。

我现在以不同的方式实现了我的偏好(使用示例here)并放弃使用PreferenceFragment。所以,感谢 Marco 让我保持理智。我现在可以继续前进了。

相关问题