我很难找到 Android API 中提供的 MultiSelectListPreference 的一个很好的例子。我已经看到很多对这个博客的引用,虽然这是我想要的最终结果,但我不想为我想要实现的每个多选首选项创建一个类。最终,我想查看一个简单的多选对话框(我将动态填充值)的首选项 xml,以及对addPreferencesFromResource(R.xml.preferences);
目前,我有:
<MultiSelectListPreference
android:defaultValue=""
android:enabled="true"
android:entries="@array/pref_default_entries"
android:entryValues="@array/pref_default_values"
android:key="TargetList"
android:persistent="true"
android:summary="@string/TargetSummary"
android:title="@string/TargetTitle" />
当我尝试在我的活动 onCreate 调用中调用 addPreferencesFromResource 时,我收到以下错误:
06-18 13:59:30.690: E/AndroidRuntime(6052): FATAL EXCEPTION: main
06-18 13:59:30.690: E/AndroidRuntime(6052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tracker/com.tracker.TrackerActivity}: android.view.InflateException: Binary XML file line #37: Error inflating class java.lang.reflect.Constructor
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1818)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.app.ActivityThread.access$500(ActivityThread.java:122)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.os.Looper.loop(Looper.java:132)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.app.ActivityThread.main(ActivityThread.java:4126)
06-18 13:59:30.690: E/AndroidRuntime(6052): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 13:59:30.690: E/AndroidRuntime(6052): at java.lang.reflect.Method.invoke(Method.java:491)
06-18 13:59:30.690: E/AndroidRuntime(6052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
06-18 13:59:30.690: E/AndroidRuntime(6052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
06-18 13:59:30.690: E/AndroidRuntime(6052): at dalvik.system.NativeStart.main(Native Method)
06-18 13:59:30.690: E/AndroidRuntime(6052): Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class java.lang.reflect.Constructor
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.GenericInflater.createItem(GenericInflater.java:397)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1366)
06-18 13:59:30.690: E/AndroidRuntime(6052): at com.tracker.TrackerActivity.onCreate(TrackerActivity.java:30)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
06-18 13:59:30.690: E/AndroidRuntime(6052): ... 11 more
06-18 13:59:30.690: E/AndroidRuntime(6052): Caused by: java.lang.reflect.InvocationTargetException
06-18 13:59:30.690: E/AndroidRuntime(6052): at java.lang.reflect.Constructor.constructNative(Native Method)
06-18 13:59:30.690: E/AndroidRuntime(6052): at java.lang.reflect.Constructor.newInstance(Constructor.java:416)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.GenericInflater.createItem(GenericInflater.java:383)
06-18 13:59:30.690: E/AndroidRuntime(6052): ... 21 more
06-18 13:59:30.690: E/AndroidRuntime(6052): Caused by: java.lang.NullPointerException
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:215)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.content.res.Resources.getTextArray(Resources.java:435)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.content.res.TypedArray.getTextArray(TypedArray.java:628)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.MultiSelectListPreference.onGetDefaultValue(MultiSelectListPreference.java:210)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.Preference.<init>(Preference.java:257)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.DialogPreference.<init>(DialogPreference.java:69)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.DialogPreference.<init>(DialogPreference.java:90)
06-18 13:59:30.690: E/AndroidRuntime(6052): at android.preference.MultiSelectListPreference.<init>(MultiSelectListPreference.java:49)
06-18 13:59:30.690: E/AndroidRuntime(6052): ... 24 more
我期待你的回复!