Java.lang.Illegal状态异常:特定子级已有父级

时间:2017-04-26 12:27:23

标签: xamarin xamarin.android

我在实现异常下面的代码时遇到异常。

Java.lang.IllegalStateException:特定子项已有父项。你必须先在孩子的父母身上调用removeView()。

using System;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using Android.App;

namespace Recycle.Droid
{

    internal class albumadapter1 : RecyclerView.Adapter
    {
        // private int[] imageid;
        // private Photo_album palbum;
        // private _Recycler _Recycler;
        private int[] imageid;
        private Activity mainActivity;

        public albumadapter1(Activity context, int[] imageid)
        {
            this.mainActivity = context;
            this.imageid = imageid;
        }
        public override int ItemCount
        {
            get
            {
                return imageid.Length;
            }
        }

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            PhotoViewHolder vh1 = holder as PhotoViewHolder;
            vh1.img.SetImageResource(imageid[position]);
            //vh.Caption.Text = palbum[position].Caption;
        }

        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            var itemview = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.view_holder, parent);
            PhotoViewHolder vh = new PhotoViewHolder(itemview);
            return vh;
        }
    }
    public class PhotoViewHolder : RecyclerView.ViewHolder
    {

        public TextView tv { get; private set; }
        public ImageView img { get; private set; }

        public PhotoViewHolder(View itemView) : base(itemView)
        {

            img = itemView.FindViewById<ImageView>(Resource.Id.imageView1);
            //  var Caption = itemView.FindViewById<TextView>(Resource.Id.textView);
            // public PhotoViewHolder(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)

        }
    }
}

请帮我解决问题。

1 个答案:

答案 0 :(得分:1)

在您的问题下面的代码中添加False将被解决

....... ... ... .. 。  public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent,int viewType)         {             var itemview = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.view_holder,parent,false);             PhotoViewHolder vh =新的PhotoViewHolder(itemview);             回报vh;         } ..... ... ..