Android Xamarin给出GetLayoutFlater的错误。不';我没读过。我怎样才能解决这个问题?

Android Xamarin给出GetLayoutFlater的错误。不';我没读过。我怎样才能解决这个问题?,android,xamarin,xamarin.android,Android,Xamarin,Xamarin.android,我目前正在使用Xamarin.Android创建一个浏览文件的活动 在Xamarin提供的示例中,他们使用GetLayoutInflater作为FileListAdapter.cs的一部分。出于某种原因,Xamarin没有注册GetLayoutInflater。我能做什么?它不能是代码,因为我只是从示例中复制 using System; using System.Collections.Generic; using System.Linq; using System.Text; using A

我目前正在使用Xamarin.Android创建一个浏览文件的活动

在Xamarin提供的示例中,他们使用GetLayoutInflater作为FileListAdapter.cs的一部分。出于某种原因,Xamarin没有注册GetLayoutInflater。我能做什么?它不能是代码,因为我只是从示例中复制

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.IO;

namespace RefrigerantID.Droid
{
    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;
        }

        /// <summary>
        ///   We provide this method to get around some of the
        /// </summary>
        /// <param name="directoryContents"> </param>
        public void AddDirectoryContents(IEnumerable<FileSystemInfo> directoryContents)
        {
            Clear();
            // Notify the _adapter that things have changed or that there is nothing 
            // to display.
            if (directoryContents.Any())
            {
if __ANDROID_11__
                // .AddAll was only introduced in API level 11 (Android 3.0). 
                // If the "Minimum Android to Target" is set to Android 3.0 or 
                // higher, then this code will be used.
                AddAll(directoryContents.ToArray());
#else
                // This is the code to use if the "Minimum Android to Target" is
                // set to a pre-Android 3.0 API (i.e. Android 2.3.3 or lower).
                lock (this)
                    foreach (var fsi in directoryContents)
                    {
                        Add(fsi);
                    }
endif

                NotifyDataSetChanged();
            }
            else
            {
                NotifyDataSetInvalidated();
            }
        }

        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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用System.IO;
名称空间制冷剂ID.Droid
{
公共类FileListAdapter:ArrayAdapter
{
私有只读上下文\u上下文;
公共FileListAdapter(上下文,IList fsi)
:base(context,Resource.Layout.file\u picker\u list\u项,Android.Resource.Id.Text1,fsi)
{
_上下文=上下文;
}
/// 
///我们提供这种方法来绕过一些
/// 
///  
public void AddDirectoryContents(IEnumerable directoryContents)
{
清除();
//通知_适配器情况已更改或没有任何更改
//展示。
if(directoryContents.Any())
{
如果__
//.AddAll仅在API级别11(Android 3.0)中引入。
//如果“最低Android目标”设置为Android 3.0或
//更高,则将使用此代码。
AddAll(directoryContents.ToArray());
#否则
//如果“Android到目标的最小值”为
//设置为Android 3.0之前的API(即Android 2.3.3或更低版本)。
锁(这个)
foreach(directoryContents中的var fsi)
{
添加(fsi);
}
恩迪夫
NotifyDataSetChanged();
}
其他的
{
notifyDataSetionValidated();
}
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
var filesystem=GetItem(位置);
FileListRowViewHolder视图持有者;
查看行;
if(convertView==null)
{
行=\u context.getLayoutFlater().Inflate(Resource.Layout.file\u picker\u list\u项,父项,false);
viewHolder=newfilelistrowviewholder(row.findviewbyd(Resource.Id.file\u picker\u text),row.findviewbyd(Resource.Id.file\u picker\u image));
row.Tag=viewHolder;
}
其他的
{
行=转换视图;
viewHolder=(FileListRowViewHolder)row.Tag;
}
更新(filesystemmentry.Name,filesystemmentry.IsDirectory()?Resource.Drawable.folder:Resource.Drawable.file);
返回行;
}
}
}


您的
上下文
是错误的

方法
getLayoutFlater
存在于
FragmentActivity
上,而
Activity
包含用于获取
LayoutFlater
的属性


Re:

看起来您将上下文传递给构造函数,然后扩展项布局

_context.GetLayoutInflater().Inflate(Resource.Layout.file_picker_list_item, parent, false);
你不必那样做。 这是我通常做的

  • 从参数
    父项
    访问上下文
  • 使用
    LayoutInflater.FromContext(parent.Context)

  • 希望能有所帮助。

    您能指导我改变什么吗?这是因为它不是“公共类MyFragment:Fragment{}”@neutJor。如果您使用的是Xamarin的示例逐字记录,那么请将
    活动更改为
    FragmentActivity
    。。。。