Search code examples
androidxamarinxamarin.androidcustom-adapter

Error on CustomAdapter that extends BaseAdapter


Hello I am trying to build a custom adapter that extends either base adapter or array adapter. But with both I receive the same errors. here is a part of my log.

Error (29111) / AndroidRuntime: Caused by: 
android.runtime.JavaProxyThrowable: System.NullReferenceException: Object 
reference not set to an instance of an object
Error (29111) / AndroidRuntime:   at app2.NoteAdapter.GetView (System.Int32 
position, Android.Views.View convertView, Android.Views.ViewGroup parent) 
[0x0002e] in <1fa21d766827459f835e8535977f72f9>:0 
Error (29111) / AndroidRuntime:   at 
Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View
_Landroid_view_ViewGroup_ (System.IntPtr jnienv, System.IntPtr native__this, 
System.Int32 position, System.IntPtr native_convertView, System.IntPtr 
native_parent) [0x00018] in <d855bac285f44dda8a0d8510b679b1e2>:0 
Error (29111) / AndroidRuntime:   at (wrapper dynamic-method) 
System.Object:f5ec6c83-541f-4cff-a749-2883ba3ae9b3 
(intptr,intptr,int,intptr,intptr)
Error (29111) / AndroidRuntime:     at 
md56e50694a3408ec27ef32796f24258eb7.NoteAdapter.n_getView(Native Method)
Error (29111) / AndroidRuntime:     at 
md56e50694a3408ec27ef32796f24258eb7.NoteAdapter.getView(NoteAdapter.java:56)
Error (29111) / AndroidRuntime:     at 
android.widget.AbsListView.obtainView(AbsListView.java:2370)
Error (29111) / AndroidRuntime:     at 
android.widget.ListView.measureHeightOfChildren(ListView.java:1284)
Error (29111) / AndroidRuntime:     at 
android.widget.ListView.onMeasure(ListView.java:1192)

The code of the custom adapter is the one below:

  public class NoteAdapter:BaseAdapter<DataModelNotes>
{
//  Context _context;
    List<DataModelNotes> _notes;
    private readonly Activity _activity;
    public NoteAdapter(Activity activity, List<DataModelNotes> notes):base()
    {
        _activity = activity;

        _notes = notes;
    }

    public override int Count
    {
        get { return _notes.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override DataModelNotes this[int position]
    {
        get { return _notes[position]; }
    }


    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        DataModelNotes note = this[position];
        var view = convertView;
            if (view == null)
        {
            view = _activity.LayoutInflater.Inflate(Resource.Layout.custom_notes_row, null);
            }
        var title = convertView.FindViewById<TextView>(Resource.Id.noteTitle);
        var summary = convertView.FindViewById<TextView>(Resource.Id.noteSummary);

        title.Text = note.Title;
        summary.Text = note.Summary;
        return view;
    }
}

}

and the only thing I am doing in my listfragment is this below:

public class NoteListFragment : ListFragment
{

    List<DataModelNotes> notes;
    public NoteListFragment()
    {
    }

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        notes = new List<DataModelNotes>();
        DataModelNotes note = new DataModelNotes();
        note.Id = 0;
        note.Title = "adaadassaddsa";
        note.Summary = "sdfsfdsfdsdfsds";
        notes.Add(note);
        NoteAdapter ad = new NoteAdapter(this.Activity as NotesMainActivity, notes);
        this.ListAdapter = ad;
        return base.OnCreateView(inflater, container, savedInstanceState);
    }

The custom layout is just a linear layout that contains 2 textviews. I can not understand what I am doing wrong. I have tried a lot of things but the error remains. Can someone give me some help?


Solution

  • You are accidentally referencing "convertView" which is probably null on the first call, right after inflating your layout and setting it as your "view" variable. So just change it to:

    var view = convertView;
    if (view == null)
    {
        view = _activity.LayoutInflater.Inflate(Resource.Layout.custom_notes_row, null);
    }
    var title = view.FindViewById<TextView>(Resource.Id.noteTitle);
    var summary = view.FindViewById<TextView>(Resource.Id.noteSummary);