Note that there are some explanatory texts on larger screens.

plurals
  1. POSharedPreferences ListPreference NullPointerException
    text
    copied!<p>I'm trying to set up a list of frequencies in my preferences xml but I keep getting this error. I have attached all the related files too. I believe that I set up the values correctly but I can't find where my error currently is. Also, when I use the SharedPreferences.getString(key, defaultValue) what value is returned? The ENTRY or the ENTRYVALUE?</p> <p>My Error:</p> <pre><code>08-20 00:14:25.195: E/AndroidRuntime(1260): FATAL EXCEPTION: main 08-20 00:14:25.195: E/AndroidRuntime(1260): java.lang.NullPointerException 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.preference.ListPreference.findIndexOfValue(ListPreference.java:215) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.preference.ListPreference.getValueIndex(ListPreference.java:224) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.preference.ListPreference.getEntry(ListPreference.java:202) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.preference.ListPreference.getSummary(ListPreference.java:148) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.preference.Preference.onBindView(Preference.java:515) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.preference.Preference.getView(Preference.java:453) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:222) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.AbsListView.obtainView(AbsListView.java:2461) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.ListView.makeAndAddView(ListView.java:1775) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.ListView.fillDown(ListView.java:678) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.ListView.fillFromTop(ListView.java:739) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.ListView.layoutChildren(ListView.java:1628) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.AbsListView.onLayout(AbsListView.java:2296) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.View.layout(View.java:14063) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewGroup.layout(ViewGroup.java:4603) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.View.layout(View.java:14063) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewGroup.layout(ViewGroup.java:4603) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.View.layout(View.java:14063) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewGroup.layout(ViewGroup.java:4603) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.View.layout(View.java:14063) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewGroup.layout(ViewGroup.java:4603) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.View.layout(View.java:14063) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewGroup.layout(ViewGroup.java:4603) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1994) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1815) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4518) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.Choreographer.doFrame(Choreographer.java:525) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.os.Handler.handleCallback(Handler.java:615) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.os.Handler.dispatchMessage(Handler.java:92) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.os.Looper.loop(Looper.java:137) 08-20 00:14:25.195: E/AndroidRuntime(1260): at android.app.ActivityThread.main(ActivityThread.java:4898) 08-20 00:14:25.195: E/AndroidRuntime(1260): at java.lang.reflect.Method.invokeNative(Native Method) 08-20 00:14:25.195: E/AndroidRuntime(1260): at java.lang.reflect.Method.invoke(Method.java:511) 08-20 00:14:25.195: E/AndroidRuntime(1260): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 08-20 00:14:25.195: E/AndroidRuntime(1260): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 08-20 00:14:25.195: E/AndroidRuntime(1260): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>xml layout:</p> <pre><code>&lt;ListPreference android:key="frequency_key" android:title="Sample Rate" android:defaultValue="8000" android:entries="@array/freq_titles" android:entryValues="@array/freq_values" /&gt; </code></pre> <p>xml array:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string-array name="freq_titles"&gt; &lt;item name="8000"&gt;8k Hz&lt;/item&gt; &lt;item name="16000"&gt;16k Hz&lt;/item&gt; &lt;item name="22050"&gt;22.05k Hz&lt;/item&gt; &lt;item name="44100"&gt;44.1k Hz&lt;/item&gt; &lt;item name="48000"&gt;48k Hz&lt;/item&gt; &lt;/string-array&gt; &lt;array name="freq_values"&gt; &lt;item name="8000"&gt;8000&lt;/item&gt; &lt;item name="16000"&gt;16000&lt;/item&gt; &lt;item name="22050"&gt;22050&lt;/item&gt; &lt;item name="44100"&gt;44100&lt;/item&gt; &lt;item name="48000"&gt;48000&lt;/item&gt; &lt;/array&gt; &lt;/resources&gt; </code></pre> <p>UPDATE: It's weird even when I do a getAll() from the default shared preferences it shows me that "frequency_key=8k Hz". So it's initialized.</p> <p>More code from my preference class:</p> <pre><code>public class Settings_ActivityAH extends PreferenceFragment implements OnSharedPreferenceChangeListener{ private SharedPreferences prefs; private Preference myAccountName; private Preference myGoogleUsername; private Preference myGooglePassword; private static Context context; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); context = getActivity(); if (context == null){ Log.e("error","context is null"); } prefs = PreferenceManager.getDefaultSharedPreferences(context); myAccountName = (Preference) findPreference("account_name_title_key"); myGoogleUsername = (Preference) findPreference("googleusername_key"); myGooglePassword = (Preference) findPreference("googlepassword_key"); myAccountName.setSummary(prefs.getString("account_name_title_key", "none")); myGoogleUsername.setTitle("username: "+prefs.getString("googleusername_key", "none")); myGooglePassword.setTitle("password: "+prefs.getString("googlepassword_key", "none")); Log.e("Check prefs",prefs.getAll().toString()); prefs.registerOnSharedPreferenceChangeListener(this); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Log.e("in onSharedPref","yea"); Preference preference = findPreference(key); if (preference.getClass() == EditTextPreference.class){ Log.e("in editText Pref", "yeah"); if( key.equals("account_name_title_key")){ myAccountName.setSummary(sharedPreferences.getString("account_name_key","none")); Log.e("accountname prefs",sharedPreferences.getString("account_name_key","none")); } if( key.equals("account_name_key")){ myAccountName.setSummary(sharedPreferences.getString("account_name_key","none")); Log.e("accountname prefs",sharedPreferences.getString("account_name_key","none")); } else if (preference.getClass() == PreferenceCategory.class){ Log.e("in category Pref", "yeah"); if (key.equals("googleusername_key")){ myGoogleUsername.setTitle("Username: "+sharedPreferences.getString(key, "none")); Log.e("googleusername prefs",sharedPreferences.getString(key, "none")); } else if( key.equals("googlepassword_key")){ myGooglePassword.setTitle("Password: "+sharedPreferences.getString(key, "none")); Log.e("googlepassword prefs",sharedPreferences.getString(key, "none")); } } } else{ Log.e("insufficient key called","onSharedPreferenceChanged AH"); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload