在PreferenceScreen中检索ListPreference

时间:2011-03-15 16:22:24

标签: android listpreference preferencescreen

如何在PreferenceScreen中检索ListPreference?

我尝试了以下内容,并且它们都抛出了null异常。

findPreference(键) 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):致命异常:主要   03-15 17:50:42.250:ERROR / AndroidRuntime(441):java.lang.RuntimeException:无法启动活动ComponentInfo {com.feral.widget / com.feral.widget.Prefs}:java.lang.NullPointerException   03-15 17:50:42.250:ERROR / AndroidRuntime(441):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)   03-15 17:50:42.250:ERROR / AndroidRuntime(441):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)   03-15 17:50:42.250:ERROR / AndroidRuntime(441):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)   03-15 17:50:42.250:ERROR / AndroidRuntime(441):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:928)   03-15 17:50:42.250:ERROR / AndroidRuntime(441):在android.os.Handler.dispatchMessage(Handler.java:99)   03-15 17:50:42.250:ERROR / AndroidRuntime(441):在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:ERROR / AndroidRuntime(441):at 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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   03-15 17:50:42.250:ERROR / AndroidRuntime(441):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)

1 个答案:

答案 0 :(得分:1)

您是否在PreferenceActivity的onCreate中调用addPreferencesFromResource(R.xml.preferences)