Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView exception using PreferenceFragmentCompat without any RecyclerView

I am using PreferenceFragmentCompat and I get this runtime exception:

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference

The confusing part is that my preference definition does not contain any RecyclerViews. Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >

<android.support.v7.preference.PreferenceCategory
    android:layout="@layout/preference_first_category"
    android:title="@string/pref_category_general">

    <android.support.v7.preference.SwitchPreferenceCompat
        android:title="@string/pref_demo_mode"
        android:key="@string/pref_demo_mode_key"
        android:defaultValue="true"
        android:persistent="true" />

</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory
    android:layout="@layout/preference_category"
    android:key="serverCategory"
    android:persistent="false"
    android:title="@string/pref_category_server">

    <android.support.v7.preference.EditTextPreference
        android:title="@string/pref_server_control_ip"
        android:summary="@string/pref_enter_ip_address"
        android:defaultValue="@string/pref_enter_ip_address"
        android:key="@string/pref_ctrl_ip_key"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:persistent="true" />

    <android.support.v7.preference.EditTextPreference
        android:title="@string/pref_server_admin_ip"
        android:summary="@string/pref_enter_ip_address"
        android:defaultValue="@string/pref_enter_ip_address"
        android:key="@string/pref_admin_ip_key"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:persistent="true" />

    <android.support.v7.preference.EditTextPreference
        android:title="@string/pref_server_network_mask"
        android:defaultValue="@string/pref_enter_network_mask"
        android:summary="@string/pref_enter_network_mask"
        android:key="@string/pref_network_mask_key"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:persistent="true" />

</android.support.v7.preference.PreferenceCategory>         
</android.support.v7.preference.PreferenceScreen>

I have review the Reference API and can clearly see that PreferenceFragmentCompat does support RecyclerView but I have seen several examples of working code that do NOT have a RecyclerView view in their preferences so it appears RecyclerView is not mandatory.

Do I need to override any RecyclerView methods? Other working examples without RecyclerView didn't need to so I am at a loss as to how to resolve this.

Thanks, -Andres

like image 590
Andres Gonzalez Avatar asked Mar 31 '17 02:03

Andres Gonzalez


1 Answers

Leaving this answer for anyone who stumbles upon it: I had this same problem. In my case the issue was that I had this:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_settings, container, false);
    }

Once I commented this out it worked: evidently PreferenceCompatFragment must inflate its own view (that probably provides the RecyclerView). If you override it and inflate your own layout it will fail.

like image 108
Femi Avatar answered Nov 14 '22 23:11

Femi