我在构建项目时遇到上述错误。方法引用的上下文不同,所以我不知道是什么导致了错误。我在app中有一个文件浏览功能。所以取决于调用的是哪个活动在浏览器中,用户可以选择文件夹或文件并返回其路径。
public class FileListAdapter : ArrayAdapter<FileSystemInfo>
{
private readonly Context _context;
public FileListAdapter(Context context, IList<FileSystemInfo> fsi)
: base(context, Resource.Layout.file_picker_list_item, Android.Resource.Id.Text1, fsi)
{
_context = context;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var fileSystemEntry = GetItem(position);
FileListRowViewHolder viewHolder;
View row;
if (convertView == null)
{
row = _context.GetLayoutInflater().Inflate(Resource.Layout.file_picker_list_item, parent, false);
viewHolder = new FileListRowViewHolder(row.FindViewById<TextView>(Resource.Id.file_picker_text), row.FindViewById<ImageView>(Resource.Id.file_picker_image));
row.Tag = viewHolder;
}
else
{
row = convertView;
viewHolder = (FileListRowViewHolder)row.Tag;
}
viewHolder.Update(fileSystemEntry.Name, fileSystemEntry.IsDirectory() ? Resource.Drawable.folder : Resource.Drawable.file);
return row;
}
}
第二课:
public class FolderListAdapter : ArrayAdapter<DirectoryInfo>
{
public override View GetView(int position, View convertView, ViewGroup parent)
{
private readonly Context _context;
public FolderListAdapter(Context context, IList<DirectoryInfo> fsi)
: base(context, Resource.Layout.file_picker_list_item, Android.Resource.Id.Text1, fsi)
{
_context = context;
}
var fileSystemEntry = GetItem(position);
FileListRowViewHolder viewHolder;
View view = convertView;
if (convertView == null)
{
view = _context.GetLayoutInflater().Inflate(Resource.Layout.folder_picker_list_item, parent, false);
viewHolder = new FileListRowViewHolder(view.FindViewById<TextView>(Resource.Id.folder_picker_text), view.FindViewById<ImageView>(Resource.Id.folder_picker_image));
view.Tag = viewHolder;
}
else
{
view = convertView;
viewHolder = (FileListRowViewHolder)view.Tag;
}
viewHolder.Update(fileSystemEntry.Name, fileSystemEntry.IsDirectory() ? Resource.Drawable.folder : Resource.Drawable.file);
return view;
}
}
这两行中的这些行都出现错误:
view = _context.GetLayoutInflater().Inflate(Resource.Layout.folder_picker_list_item, parent, false);
答案 0 :(得分:0)
我解决了构建错误。这是一个很小的问题。上下文是问题。所以我只是改变了有问题的语境&#39;指向Activity.From this:
public static LayoutInflater GetLayoutInflater(this Context context)
{
return context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
}
对此:
public static LayoutInflater GetLayoutInflater(Activity context)
{
return context.GetSystemService(Activity.LayoutInflaterService).JavaCast<LayoutInflater>();
}