Note that there are some explanatory texts on larger screens.

plurals
  1. POPreferenceActivity gives a NullPointerException when option selected from ListPreference
    primarykey
    data
    text
    <p>I'm new to using the PreferenceActivity.</p> <p><strong>Task:</strong> Allow user to choose the program layout from the Preferences</p> <p><strong>Problem:</strong> Selecting an option in the PreferenceList causes a NullPointerException</p> <p><strong>Exception arises:</strong> At android.preference.ListPreference.onDialogClosed()</p> <p><img src="https://i.stack.imgur.com/1Wdy1.png" alt="alt text"></p> <p>(Shortened) Code:</p> <pre><code>private static final String PREF_LAYOUT_KEY = "PrefLayout"; private static final int DEFAULT_LAYOUT = LayoutHelper.LAYOUT_DOUBLE ; private static int mListLayout = DEFAULT_LAYOUT ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); pref.registerOnSharedPreferenceChangeListener(this); mListLayout = pref.getInt(PREF_LAYOUT_KEY, DEFAULT_LAYOUT); } @Override public void onSharedPreferenceChanged(SharedPreferences pref, String key) { Log.v(TAG, "OnSharedPreferencesChanged run" ); // TODO Testing Purposes if( key.equals( PREF_LAYOUT_KEY ) ){ mListLayout = pref.getInt(key, DEFAULT_LAYOUT); } } </code></pre> <p>[PreferenceActivity]</p> <pre><code>public class Preferences extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.pref); } } </code></pre> <p>[res/xml/pref.xml]</p> <p> </p> <pre><code>&lt;PreferenceCategory android:title="@string/pref_cat1_title"&gt; &lt;ListPreference android:title="@string/pref_layout_name" android:summary="@+id/pref_chosen_layout" android:key="PrefLayout" android:entries="@array/prefLayoutOptions" android:entryValues="@array/prefLayoutOptionsValues" /&gt; &lt;/PreferenceCategory&gt; </code></pre> <p></p> <p>[Strings.xml]</p> <pre><code>&lt;string name="pref_cat1_title"&gt;Layout&lt;/string&gt; &lt;string name="pref_layout_name"&gt;"Layout of list"&lt;/string&gt; &lt;array name="prefLayoutOptions"&gt; &lt;item&gt;Layout 1 (single)&lt;/item&gt; &lt;item&gt;Layout 2 (double)&lt;/item&gt; &lt;item&gt;Layout 3 (triple)&lt;/item&gt; &lt;item&gt;Layout 4 (quad)&lt;/item&gt; &lt;/array&gt; &lt;array name="prefLayoutOptionsValues"&gt; &lt;item&gt;50&lt;/item&gt; &lt;item&gt;51&lt;/item&gt; &lt;item&gt;52&lt;/item&gt; &lt;item&gt;53&lt;/item&gt; &lt;/array&gt; </code></pre> <p>The bit of code that logs OnSharedPreferencesChanged being run never gets there.</p> <p>Can anybody see where I've gone wrong?</p> <p>EDIT. Here's the top of the stack trace:</p> <pre><code>E/AndroidRuntime( 2707): java.lang.NullPointerException E/AndroidRuntime( 2707): at android.preference.ListPreference.onDialogClosed(ListPreference.java:218) E/AndroidRuntime( 2707): at android.preference.DialogPreference.onDismiss(DialogPreference.java:384) E/AndroidRuntime( 2707): at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1047) E/AndroidRuntime( 2707): at android.os.Handler.dispatchMessage(Handler.java:99) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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