Domanda

Come faccio a recuperare un ListPreference dentro il mio PREFERENCE?

Ho provato con il seguente, e tutti e due tiri eccezione null.

findPreference (chiave) getPreferenceScreen (). findPreference (tasto)

private void loadLayout(int appWidgetId) {
SharedPreferences.Editor shared_config = prefs.edit();
Layout layout = db_adapter.fetchLayout(appWidgetId);

...

ListPreference widget_text_color = (ListPreference) findPreference("widget_text_color");
widget_text_color.setValue(String.valueOf(layout.getTextColor()));

...
shared_config.commit();

}

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
...
<PreferenceCategory android:title="Layout/Output">
    ...
    <ListPreference
        android:key="widget_text_color"
        android:title="Text color"
        android:summary="Choose text color"
        android:entries="@array/colors"
        android:entryValues="@array/colors_rgb"
        android:dialogTitle="Choose a color" />
    ...
</PreferenceCategory>

03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): eccezione irreversibile: main 03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): java.lang.RuntimeException: Impossibile avviare l'attività ComponentInfo {com.feral.widget / com.feral.widget.Prefs}: java.lang.NullPointerException 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1622) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:1638) 03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): a android.app.ActivityThread.access $ 1500 (ActivityThread.java:117) 03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): a android.app.ActivityThread $ H.handleMessage (ActivityThread.java:928) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a android.os.Handler.dispatchMessage (Handler.java:99) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a android.os.Looper.loop (Looper.java:123) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a android.app.ActivityThread.main (ActivityThread.java:3647) 03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): a java.lang.reflect.Method.invokeNative (metodo natale) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a java.lang.reflect.Method.invoke (Method.java:507) 03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): a com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:839) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a com.android.internal.os.ZygoteInit.main (ZygoteInit.java:597) 03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): a dalvik.system.NativeStart.main (metodo natale) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): causato da: java.lang.NullPointerException 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a com.feral.widget.Prefs.loadLayout (Prefs.java:234) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a com.feral.widget.Prefs.loadPreference (Prefs.java:267) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a com.feral.widget.Prefs.onCreate (Prefs.java:77) 03-15 17: 50: 42.250: ERROR / AndroidRuntime (441): a android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047) 03-15 17: 50: 42.250: ERRORE / AndroidRuntime (441): a android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1586)

È stato utile?

Soluzione

Stai chiamando addPreferencesFromResource(R.xml.preferences) nella vostra onCreate del PreferenceActivity?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top