当前位置:首页 - 博客 - 正文

ViewHolder views must not be attached when created

java.lang.IllegalStateException: ViewHolder views must not be attached when created.
这说明onCreateViewHolder 方法写错了
这句话的意思是,viewHolder不予许在创建的时候添加到parent里,所以如果在onCreateViewHolder方法里使用:

  1. View view = inflater.inflate(R.layout.item_view, parent, true);
  2. 或者
  3. View view = inflater.inflate(R.layout.item_view, parent);

就会报错:

  1. ViewHolder views must not be attached when created. Ensure that you are not passing true to the attachToRoot parameter of LayoutInflate

只要改成:

  1. // View view = inflater.inflate(R.layout.item_view, null); 或者
  2. // View view = inflater.inflate(R.layout.item_view, parent, false);
  3. View rootView = LayoutInflater.from(CheckInActivity.this).inflate( R.layout.layout_check_in_week, parent,false);