RecyclerView$Adapter null AndoirdX.Preference

Ferron Nijland 121 Reputation points
2021-03-26T15:21:30.99+00:00

I'm trying to setup a preference sceen with AndroidX.Preference.
But i'm getting this error when opening the fragment containing the preference:

Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference'

PreferenceFragment:

using Android.OS;
using Android.Views;
using AndroidX.Preference;

namespace GeoTrack.Android
{ 
    public class PreferenceServerauthFragment : PreferenceFragmentCompat
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            return inflater.Inflate(Resource.Layout.preference_serverauth_fragment, container, false);

            //return base.OnCreateView(inflater, container, savedInstanceState);
        }

        public override void OnCreatePreferences(Bundle savedInstanceState, string rootKey)
        {
            SetPreferencesFromResource(Resource.Xml.preference_serverauth, rootKey);
        }
    }
}

The rootkey is null.
Resource\xml\preference.xml:

<PreferenceScreen
    xmlns:app="http://schemas.android.com/apk/res-auto">

  <SwitchPreferenceCompat
      app:key="notifications"
      app:title="Enable message notifications"/>

</PreferenceScreen>
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,376 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ferron Nijland 121 Reputation points
    2021-03-26T16:14:26.447+00:00

    Needed to delete the old:

         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
         {
             // Use this to return your custom view for this Fragment
             return inflater.Inflate(Resource.Layout.preference_serverauth_fragment, container, false);
    
             //return base.OnCreateView(inflater, container, savedInstanceState);
         }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.