質問

I am currently trying to implement a MultiSelectListPreference into my settings but encounter the following error:

E/ActivityThread: Failed to inflate
        android.view.InflateException: Binary XML file line #18: Error inflating class java.lang.reflect.Constructor
        at android.preference.GenericInflater.createItem(GenericInflater.java:397)
        at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
        at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
        at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
        at android.preference.GenericInflater.inflate(GenericInflater.java:326)
        at android.preference.GenericInflater.inflate(GenericInflater.java:263)
        at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
        at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
        at com.mikebdev.refuel.SettingsActivity$SettingsFragment.onCreate(SettingsActivity.java:34)
        at android.app.Fragment.performCreate(Fragment.java:1673)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
        at android.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
        at android.app.Activity.performStart(Activity.java:5113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
        at android.app.ActivityThread.access$600(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5227)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        at android.preference.GenericInflater.createItem(GenericInflater.java:383)
        ... 26 more
        Caused by: java.lang.NullPointerException
        at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:230)
        at android.content.res.Resources.getTextArray(Resources.java:427)
        at android.content.res.TypedArray.getTextArray(TypedArray.java:628)
        at android.preference.MultiSelectListPreference.onGetDefaultValue(MultiSelectListPreference.java:211)
        at android.preference.Preference.<init>(Preference.java:267)
        at android.preference.DialogPreference.<init>(DialogPreference.java:69)
        at android.preference.DialogPreference.<init>(DialogPreference.java:90)
        at android.preference.MultiSelectListPreference.<init>(MultiSelectListPreference.java:49)
        ... 29 more

My XML looks like this:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <ListPreference
            android:defaultValue="@string/pref_amountUnit_default"
            android:dialogTitle="@string/pref_amountUnit"
            android:entries="@array/pref_amountUnit_entries"
            android:entryValues="@array/pref_amountUnit_values"
            android:key="pref_amountUnit"
            android:summary="@string/pref_amountUnit_summary"
            android:title="@string/pref_amountUnit"/>
    <ListPreference
            android:defaultValue="@string/pref_distanceUnit_default"
            android:dialogTitle="@string/pref_distanceUnit"
            android:entries="@array/pref_distanceUnit_entries"
            android:entryValues="@array/pref_distanceUnit_values"
            android:key="pref_distanceUnit"
            android:summary="@string/pref_distanceUnit_summary"
            android:title="@string/pref_distanceUnit"/>
    <MultiSelectListPreference
            android:key="pref_consumptionUnit"
            android:title="@string/pref_consumptionUnit"
            android:summary="@string/pref_consumptionUnit_summary"
            android:dialogTitle="@string/pref_consumptionUnit"
            android:entries="@array/pref_consumptionUnit_entries"
            android:entryValues="@array/pref_consumptionUnit_values"
            android:defaultValue="@string/pref_consumptionUnit_default"
            />
    <EditTextPreference
            android:defaultValue="@string/pref_moneyUnit_default"
            android:dialogTitle="@string/pref_moneyUnit"
            android:key="pref_moneyUnit"
            android:summary="@string/pref_moneyUnit_summary"
            android:title="@string/pref_moneyUnit"/>
</PreferenceScreen>

I have no Idea what is causing this error. I hope you can point me to the right direction

役に立ちましたか?

解決 2

OK I just got it.

I was still writing a default value into my SharedPreferences at start from when I was using the ListPreference. This obviously does not work with the Set the MultiSelectListPreference wants to create.

I just changed getString to getStringSet when trying to fetch the preference and created a StringSet for the default value:

Set<String> consumptionUnits = new HashSet<String>();
        consumptionUnits.add("l/km");
consumption_unit = sharedPref.getStringSet("pref_consumptionUnit", consumptionUnits);

他のヒント

Also had the NPE on MultiSelectListPreference. In API 22 all good, in API 15 a NPE.

After trail and error the trigger was the summary in XML. After setting the summary programmatically, no NPE.

Seems like a bug in the app compat library?

It looks like the following attribute for your MultiSelectListPreference is causing the NullPointerException (while trying to resolve the String?)

android:defaultValue="@string/pref_consumptionUnit_default"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top