你好我正在試圖構建一個自定義的適配器來擴展基本適配器或者陣列適配器。但是,我都收到了同樣的錯誤。這是我日誌的一部分。在擴展BaseAdapter的CustomAdapter上的錯誤
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)
定製適配器的代碼是下面的一個:
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;
}
}
}
,我在我的listfragment做的唯一事情就是下面這樣:
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);
}
自定義佈局只是包含2個textview的線性佈局。我無法理解我做錯了什麼。我嘗試了很多東西,但錯誤依然存在。有人能給我一些幫助嗎?
我已經從日誌中提供了更多信息。它在第56行中表示,但第56行是我的NoteAdapter類的結尾 – Akis
在GetView方法的第一行上設置一個斷點並逐行逐行並對每個變量求值爲空........ – SushiHangover