我已经在 SO 上看到了与此问题相关的所有问题,但没有一个对我有用。我正在尝试删除从PreferenceFragment
类中的 xml 文件加载的首选项,但它并没有删除该首选项。
这是我的pref_screen.xml文件
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<! ------------ />
<Preferencecategory>
<PreferenceCategory
android:key="device_section">
<Preference
android:key="sound"
android:icon="@drawable/ic_settings_icon"
android:title="@string/sound">
</Preference>
<Preference
android:key="storage_settings"
android:title="@string/storage_settings"
android:icon="@drawable/ic_settings_storage">
<Preference>
</PreferenceCategory>
<PreferenceCategory>
<! ------------------ />
</PreferenceCategory>
</PreferenceScreen>
这是我的PreferenceFragment类:
public static class PrefFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_screen);
final Preference storage = getPreferenceScreen().findPreference("storage_settings");
if(storage != null){
Log.d("SettingsActivity : ", "findPreference(storage_settings) not returning null.");
}
final PreferenceCategory device_prefCat =
(PreferenceCategory) getPreferenceScreen().findPreference("device_section");
if(device_prefCat != null){
Log.d("SettingsActivity : " , "findPreference(device_section) not returning null.");
}
if(device_prefCat.removePreference(storage)){
Log.d("SettingsActivity : ", "device_prefCat() returns true.");
}else{
Log.d("SettingsActivity : ", "device_prefCat() returns false");
}
}
我检查了日志。findPreference()
不是返回null
,而是removePreference()
返回false
。请告诉我我做错了什么。