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

时间:2017-03-31 02:47:03

标签: 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

令人困惑的部分是我的偏好定义不包含任何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>

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

我是否需要覆盖任何RecyclerView方法?没有RecyclerView的其他工作示例不需要这样,我不知道如何解决这个问题。

谢谢, -Andres

1 个答案:

答案 0 :(得分:6)

把这个答案留给偶然发现它的人:我也有同样的问题。就我而言,问题是我有这个问题:

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

一旦我对此进行了评论,它就起作用了:显然PreferenceCompatFragment必须膨胀其自己的视图(可能提供RecyclerView)。如果您覆盖它并膨胀自己的布局,它将失败。