This is so frustrating. Why am I getting null in the search for preferences in the PreferenceActivity class in the following code?
private void setupSpecialPrefClickListener(SettingsSystem settingsSystem) { Map<String,?> keys = PreferenceManager.getDefaultSharedPreferences(this).getAll(); for(Map.Entry<String,?> entry : keys.entrySet()){ String key = entry.getKey(); if( key.startsWith("d_")) { Preference p = findPreference((CharSequence)key);
The name and prefix of the pref name is the completely legal name of the existing pref ("d_lightSensorLimit" is the exact match of the first on the map and causing an exception). And before that, he was well versed in a simple text string in the constructor. This method is also called from the constructor (replaced a huge list of entries there with a call to this method):
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings_system); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); setupSpecialPrefClickListener(this); }
At the user's request, an excerpt from settings_system.xml is given here:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <Preference android:defaultValue="0" android:dialogTitle="Night mode on lux (0-disable)" android:inputType="phone" android:key="d_lightSensorLimit" android:layout="@layout/pref" android:title="Automatic nightmode" /> </PreferenceScreen>
I am aiming for 2.1, so an outdated method is used. And it worked just fine.
source share