在listview中填充数据时获取空引用异常

时间:2012-12-11 21:48:55

标签: java for-loop android-listview mono android-widget

我想为listview构建自定义列表适配器。我引用了http://www.wrox.com/WileyCDA/中的代码。

我正在使用Mono for Android。

我有这样的课程:

namespace AnimalList
{
    public class Animal
    {
        public string Name
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public int Image
        {
            get;
            set;
        }
    }

列出适配器如下:

namespace AnimalList
{
    public class AnimalListAdapter : BaseAdapter<Animal>
    {
        Activity context;
        public List<Animal> Animals;

        public AnimalListAdapter(Activity context, List<Animal> animals)
            : base()
        {
            this.context = context;
            this.Animals = animals;
        }

        public override int Count
        {
            get { return this.Animals.Count; }
        }

        public override Animal this[int position]
        {
            get { return this.Animals[position]; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            //Get our object for this position
            var item = this.Animals[position];

            //Try to reuse convertView if it's not  null, otherwise inflate it from our item layout
            // This gives us some performance gains by not always inflating a new view
            // This will sound familiar to MonoTouch developers with UITableViewCell.DequeueReusableCell()
            var view = convertView;

            if (convertView == null || !(convertView is LinearLayout))
                view = context.LayoutInflater.Inflate(Resource.Layout.AnimalItem, parent, false);

            //Find references to each subview in the list item's view
            var imageItem = view.FindViewById(Resource.Id.imageItem) as Android.Widget.ImageView;
            var textTop = view.FindViewById(Resource.Id.textTop) as Android.Widget.TextView;
            var textBottom = view.FindViewById(Resource.Id.textBottom) as Android.Widget.TextView;

            //Assign this item's values to the various subviews
            imageItem.SetImageResource(item.Image);
            textTop.SetText(item.Name, TextView.BufferType.Normal);
            textBottom.SetText(item.Description, TextView.BufferType.Normal);

            //Finally return the view
            return view;
        }

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

并将活动列为:

    [Activity(Label = "Animal List", MainLauncher = true)]
 public class AnimalListActivity : ListActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        this.ListAdapter = new AnimalListAdapter(this,
                                                 new List<Animal>() {
            new Animal() { Name = "Elephant", 
                Description = "Big and Gray, but what the hey", 
                Image = Resource.Drawable.Icon },
            new Animal() { Name = "Chinchilla", 
                Description = "Little people of the andes", 
                Image = Resource.Drawable.Icon },
            new Animal() { Name = "Lion", 
                Description = "Cowardly lion, anyone?", 
                Image = Resource.Drawable.Icon },
            new Animal() { Name = "Skunk", 
                Description = "Ello, baby. I am ze locksmith...",
                Image = Resource.Drawable.Icon },
            new Animal() { Name = "Rhino", 
                Description = "Most live to about 60!", 
                Image = Resource.Drawable.Icon },
            new Animal() { Name = "Zebra", 
                Description = "Stripes, not so great for hiding", 
                Image = Resource.Drawable.Icon }
        });         
    }
}}

我在填充列表时遇到异常:

System.NullReferenceException: Object reference not set to an instance of an object.

我没有得到什么问题?

0 个答案:

没有答案