Android RecyclerView 报错:java.lang.IllegalStateException: The specified child already has a...

时间:2022-03-17 20:33:47

报错信息:

   java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

解决办法:

由原来

 @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(mInflater.inflate(
                R.layout.item, parent));
        return holder;
    }

改为:

  @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(mInflater.inflate(
                R.layout.item, parent,false));
        return holder;
    }