gpt4 book ai didi

android - 在 PreferenceScreen 中检索 ListPreference

转载 作者:行者123 更新时间:2023-11-30 04:44:26 24 4
gpt4 key购买 nike

如何在我的 PreferenceScreen 中检索 ListPreference?

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

查找首选项(键)getPreferenceScreen().findPreference(键)

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: ERROR/AndroidRuntime(441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 03-15 17:50:42.250: ERROR/AndroidRuntime(441): at 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): at 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): Caused by: 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): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)

最佳答案

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

关于android - 在 PreferenceScreen 中检索 ListPreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com