Domanda

Sono nuovo di utilizzare il PreferenceActivity.

Task: Consenti all'utente di scegliere il layout del programma da Preferenze

Problema: La selezione di un'opzione nel PreferenceList provoca una NullPointerException

Eccezione si pone: in android.preference.ListPreference.onDialogClosed ()

alt text

(abbreviato) Codice:

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);
    }
}

[PreferenceActivity]

public class Preferences extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.pref);
    }
}

[res / xml / pref.xml]

<PreferenceCategory
    android:title="@string/pref_cat1_title">

    <ListPreference
        android:title="@string/pref_layout_name"
        android:summary="@+id/pref_chosen_layout"
        android:key="PrefLayout"
        android:entries="@array/prefLayoutOptions"
        android:entryValues="@array/prefLayoutOptionsValues"
    />
</PreferenceCategory>

[strings.xml]

<string name="pref_cat1_title">Layout</string>
    <string name="pref_layout_name">"Layout of list"</string>
        <array name="prefLayoutOptions">
            <item>Layout 1 (single)</item>
            <item>Layout 2 (double)</item>
            <item>Layout 3 (triple)</item>
            <item>Layout 4 (quad)</item>
        </array>
        <array name="prefLayoutOptionsValues">
            <item>50</item>
            <item>51</item>
            <item>52</item>
            <item>53</item>
        </array>

Il bit di codice che i registri OnSharedPreferencesChanged in esecuzione mai arriva.

Qualcuno può vedere dove ho sbagliato andato?

EDIT. Ecco la parte superiore della traccia dello stack:

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)
È stato utile?

Soluzione

mListLayout = pref.getInt(PREF_LAYOUT_KEY, DEFAULT_LAYOUT);

che fallirà, perché la preferenza è una stringa, non un int.

<array name="prefLayoutOptions">
<array name="prefLayoutOptionsValues">

Cambia questi per <string-array>. Ecco un progetto di esempio dimostrare l'uso di un ListPreference.

Altri suggerimenti

Ho incontrato lo stesso problema. Sembra che è possibile utilizzare solo valori di stringa: http://code.google.com/p/android/issues/detail ? id = 2096

/ Håkan

Credo che è necessario impostare il cambiamento preferenza ascoltatore per le tue preferenze proprio alla attività, provare a mettere che a onCreate la propria attività:

    Map<String, ?> prefs = this.getPreferenceManager().getSharedPreferences().getAll();
    for (String preferenceName : prefs.keySet()) {
        Preference p = this.findPreference(preferenceName);
        if (p != null) {
            Object value = prefs.get(preferenceName);
            p.setOnPreferenceChangeListener(this);
        }
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top