1

如何在 PreferenceScreen 中检索 ListPreference?

我尝试过以下方法,它们都抛出空异常。

findPreference(key) getPreferenceScreen().findPreference(key)

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: ERROR/AndroidRuntime(441): FATAL EXCEPTION: main 03-15 17:50:42.250: ERROR/AndroidRuntime(441): java.lang.RuntimeException: Unable to start activity ComponentInfo{com. feral.widget/com.feral.widget.Prefs}:java.lang.NullPointerException 03-15 17:50:42.250:错误/AndroidRuntime(441):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)03 -15 17:50:42.250: 错误/AndroidRuntime(441): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 03-15 17:50:42.250: 错误/AndroidRuntime(441): 在 android.app .ActivityThread.access$1500(ActivityThread.java:117) 03-15 17:50:42.250: 错误/AndroidRuntime(441): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 03-15 17: 50:42.250:错误/AndroidRuntime(441):在 android.os.Handler。dispatchMessage(Handler.java:99) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.os.Looper.loop(Looper.java:123) 03-15 17:50:42.250: ERROR/ AndroidRuntime(441): 在 android.app.ActivityThread.main(ActivityThread.java:3647) 03-15 17:50:42.250: 错误/AndroidRuntime(441): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at java.lang.reflect.Method.invoke(Method.java:507) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:597) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at dalvik.system.NativeStart.main(Native Method) 03-15 17:50:42.250: ERROR/AndroidRuntime(441):引起:java.lang.NullPointerException 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at com.feral.widget.Prefs.loadLayout(Prefs.java:234) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at com.feral.widget.Prefs.loadPreference(Prefs.java:267) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at com.feral.widget.Prefs.onCreate (Prefs.java:77) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-15 17:50:42.250: ERROR/AndroidRuntime (441): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)loadPreference(Prefs.java:267) 03-15 17:50:42.250: 错误/AndroidRuntime(441): at com.feral.widget.Prefs.onCreate(Prefs.java:77) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java :1586)loadPreference(Prefs.java:267) 03-15 17:50:42.250: 错误/AndroidRuntime(441): at com.feral.widget.Prefs.onCreate(Prefs.java:77) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java :1586)

4

1 回答 1

1

你在调用addPreferencesFromResource(R.xml.preferences)PreferenceActivity 的 onCreate 吗?

于 2011-03-15T16:29:06.610 回答