没有任何RecyclerView的使用PreferenceFragmentCompat的RecyclerView异常

And*_*lez 6 android android-fragments android-recyclerview

我正在使用PreferenceFragmentCompat,并且出现以下运行时异常:

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

令人困惑的是,我的首选项定义不包含任何RecyclerViews。这是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>
Run Code Online (Sandbox Code Playgroud)

我已经查看了Reference API,并且可以清楚地看到PreferenceFragmentCompat确实支持RecyclerView,但是我看到了几个工作代码示例,这些示例在其首选项中没有RecyclerView视图,因此RecyclerView不是强制性的。

我是否需要重写任何RecyclerView方法?没有RecyclerView的其他工作示例则不需要,因此我对如何解决此问题感到困惑。

谢谢,-安德烈斯

Fem*_*emi 9

将此答案留给偶然发现它的任何人:我也遇到同样的问题。就我而言,问题是我有这个问题:

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

一旦我将其注释掉,它就会起作用:显然PreferenceCompatFragment必须膨胀自己的视图(可能提供RecyclerView)。如果您覆盖它并膨胀自己的布局,它将失败。