PreferenceFragmentCompat自定义布局

Lak*_*uri 12 android android-support-library preferencefragment

我需要PreferenceFragmentCompat的自定义布局.在PreferenceFragmentCompat的文档中,您似乎可以在onCreateView()中膨胀并返回一个视图.

然而,NPE结果: -

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
                                                                       at android.support.v7.preference.PreferenceFragmentCompat.bindPreferences(PreferenceFragmentCompat.java:511)
                                                                       at android.support.v7.preference.PreferenceFragmentCompat.onActivityCreated(PreferenceFragmentCompat.java:316)
                                                                       at com.cls.example.MyPrefFrag.onActivityCreated(MyPrefFrag.java:42) 
Run Code Online (Sandbox Code Playgroud)

在我检查了PreferenceFragmentCompat:onCreateView的源代码后,我发现了以下代码: -

 RecyclerView listView = this.onCreateRecyclerView(themedInflater, listContainer, savedInstanceState);
 if(listView == null) {
    throw new RuntimeException("Could not create RecyclerView");
 } else {
    this.mList = listView;   //problem
    ...
    return view;
 }
Run Code Online (Sandbox Code Playgroud)

因此,如果覆盖onCreateView()并返回自定义布局,则不会调用onCreateRecyclerView(),并且不会设置RecyclerView私有字段mList.因此,setAdapter()上的NPE结果.

我是否应该假设自定义布局对PreferenceFragmentCompat不可行?

Ken*_*ndt 10

您可以在主题中指定自定义布局.

例如:

styles.xml

<style name="YourTheme" parent="Theme.AppCompat">
    <!-- ... -->
    <item name="preferenceTheme">@style/YourTheme.PreferenceThemeOverlay</item>
</style>

<style name="YourTheme.PreferenceThemeOverlay" parent="@style/PreferenceThemeOverlay">
    <item name="android:layout">@layout/fragment_your_preferences</item>
</style>
Run Code Online (Sandbox Code Playgroud)

fragment_your_preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/custom_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <!-- Required ViewGroup for PreferenceFragmentCompat -->
    <FrameLayout
        android:id="@android:id/list_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后在onViewCreated()您的片段类中,您可以开始使用视图:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);

    Toolbar toolbar = (Toolbar)view.findViewById(R.id.custom_toolbar);

    if (toolbar != null)
    {
        getMainActivity().setSupportActionBar(toolbar);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 别客气!如果将来遇到类似的问题,可以在Android Studio中打开类似问题的类“ PreferenceFragmentCompat.class”。当您打开类文件时,它应显示(反编译)可读的Java代码。如果看不到Java代码,请在Android Studio设置中安装“ Java字节码反编译器”插件。之后,您可以开始研究问题所在:在这种情况下,您可以看到`PreferenceFragmentCompat.onCreateView`具有可样式化的布局,并查找`R.id.listContainer`。 (2认同)