Java 为什么我的Android偏好没有更新?
我在一个xml文件中设置了一些首选项,并使用标准结构(见下面的代码)使它们可用并可更新,所有这些都工作正常。由于这些都是出于测试目的,我现在想用我的实际应用程序首选项替换它们,包括完全删除一些,但无论我做什么,似乎都只能识别我设置的原始应用程序。我甚至用我的新文件完全替换了xml文件,但它仍然只找到旧的首选项。我还清除了提交后代码映射大小为0所示的条目。此外,它似乎不允许我更改“preferences.xml”的xml文件名。代码显示了我的新名称preferences_new.xml,但这在Eclipse中引发了一个错误 在调用活动onCreate时:Java 为什么我的Android偏好没有更新?,java,android,xml,sharedpreferences,Java,Android,Xml,Sharedpreferences,我在一个xml文件中设置了一些首选项,并使用标准结构(见下面的代码)使它们可用并可更新,所有这些都工作正常。由于这些都是出于测试目的,我现在想用我的实际应用程序首选项替换它们,包括完全删除一些,但无论我做什么,似乎都只能识别我设置的原始应用程序。我甚至用我的新文件完全替换了xml文件,但它仍然只找到旧的首选项。我还清除了提交后代码映射大小为0所示的条目。此外,它似乎不允许我更改“preferences.xml”的xml文件名。代码显示了我的新名称preferences_new.xml,但这在Ec
// 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的使用。所以,谢谢马可让我保持清醒。我现在可以继续。等等,保存首选项的XML与您显示的首选项片段布局不同。你好,Marco。你能澄清一下你的意思吗。谢谢您想从XML布局文件中删除所有首选项吗?R.xml.preferences\u new-我的意思是:R.xml.preferences\u new是一件事,SharedPreferences是另一件事。XML R.XML.preferences\u new不是保存SharedReference的地方。它是应用程序数据中的另一个XML。所以当你做e.clear;您只需删除这个私有共享首选项xml文件中的每个首选项,而不是R.xml.preferences\u newOK我想我理解这一点,但我添加了clear etc以尝试清除旧首选项并替换为新首选项,即旧xml下列出的首选项仍然显示在我的首选项UI屏幕上,即使我已经替换了它使用新的xml。也许我误解了整个过程。如果问题不清楚,我很抱歉。现在我很困惑。我需要的是两个持久的首选项字段:一个复选框和一个edittext,并去掉所有其他字段!