gregkorossy/Android-Support-Preference-V7-Fix

Blank Preferences Screen

Closed this issue · 5 comments

For some reason I being shown a blank preferences screen.

The fragment is loading correctly as I can set the background programatically to a different colour, however, nothing else is shown.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <PreferenceCategory android:title="First category">
        <Preference
            android:persistent="false"
            android:summary="This is just a normal preference"
            android:title="Preference" />
        <Preference
            android:enabled="false"
            android:persistent="false"
            android:summary="This is just a disabled preference"
            android:title="Preference 2" />
        <Preference
            android:icon="@drawable/ic_food_confirmed"
            android:persistent="false"
            android:summary="This is a preference with icon"
            android:title="Preference 3" />
        <Preference
            android:persistent="false"
            android:summary="This is just a preference with its icon space reserved"
            android:title="Preference 4"
            app:iconSpaceReserved="true" />
        <CheckBoxPreference
            android:persistent="false"
            android:summary="It\'s a checkbox"
            android:title="CheckBoxPreference" />
        <SwitchPreference
            android:persistent="false"
            android:summary="It\'s a switch"
            android:title="SwitchPreference" />
    </PreferenceCategory>
class FragmentSettings : PreferenceFragmentCompatDividers() {

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String) {
    setPreferencesFromResource(R.xml.preferences, rootKey)
    // additional setup
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
    try {
        val v = super.onCreateView(inflater, container, savedInstanceState)!!
        context?.let { v.setBackgroundColor(ContextCompat.getColor(it, R.color.background)) }
        v.isClickable = true
        return v
    } finally {
        setDividerPreferences(DIVIDER_PADDING_CHILD or DIVIDER_CATEGORY_AFTER_LAST or DIVIDER_CATEGORY_BETWEEN)
    }
}
}

<!-- Base application theme. -->
<style name="AppTheme" parent="@style/PreferenceFixTheme.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Check the logcat, you'll probably see a caught exception there (it's getting caught in the PreferenceFragmentCompat's onCreate(...) as of now but I'll remove the try...catch block).

Ahh yes, I missed this somehow, I'm getting:-

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter rootKey
12-05 19:31:53.816 10344-10344/ W/System.err: at FragmentSettings.onCreatePreferencesFix(FragmentSettings.kt:0)

The rootKey param is not annotated with @NonNull, so it can be null. You should update your implementation so that it is reflected (mind the ? after String in the params):

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
    setPreferencesFromResource(R.xml.preferences, rootKey)
    // additional setup
}

Did it solve your problem?