簡體   English   中英

為什么我的Android偏好設置沒有更新?

[英]Why are my Android Preferences not updating?

我在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>

我幾乎處於“拔頭發”階段,因此對您的幫助將不勝感激

現在,我認為我無法替換首選項文件的原因是,由於某種原因導致無法重新生成R.java 我放棄了對此的調查(在此之后 ),並返回到我以前的工作版本,並重新應用了更改,直到在使我的應用程序恢復工作的首選項工作之前(顯示了保持代碼版本的重要性)。 對我來說,這是一個更快的選擇。

現在,我以不同的方式實現了我的首選項(使用此處的示例),並放棄了PreferenceFragment的使用。 因此,感謝Marco使我保持理智。 我現在可以繼續前進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM