您好我正在尝试构建一个扩展基本适配器或数组适配器的自定义适配器。但两者都收到了同样的错误。这是我日志的一部分。
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个textviews的线性布局。我无法理解我做错了什么。我已经尝试了很多东西,但错误仍然存在。有人可以给我一些帮助吗?
答案 0 :(得分:2)
您无意中引用了&#34; convertView&#34;第一次调用时可能是null,在放大布局并将其设置为&#34; view&#34;之后。变量。所以只需将其更改为:
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);