material-components/material-components-android · error · IllegalStateException

Didn't find ListItemLayout in root itemView or among itemVie

Error message

Didn't find ListItemLayout in root itemView or among itemView's children.

What it means

ListItemViewHolder's constructor locates the ListItemLayout it operates on: it checks whether the itemView itself is a ListItemLayout, otherwise scans the itemView's direct children for one. If none is found, the holder cannot bind swipe/appearance logic, so it throws IllegalStateException — this is a structural contract violation between your item layout and the holder.

Source

Thrown at lib/java/com/google/android/material/listitem/ListItemViewHolder.java:51

  public ListItemViewHolder(@NonNull View itemView) {
    super(itemView);
    listItemLayout = findListItemLayout();
  }

  @NonNull
  private ListItemLayout findListItemLayout() {
    if (itemView instanceof ListItemLayout) {
      return (ListItemLayout) itemView;
    } else if (itemView instanceof ViewGroup) {
      int childCount = ((ViewGroup) itemView).getChildCount();
      for (int i = 0; i < childCount; i++) {
        View child = ((ViewGroup) itemView).getChildAt(i);
        if (child instanceof ListItemLayout) {
          return (ListItemLayout) child;
        }
      }
    }
    throw new IllegalStateException(
        "Didn't find ListItemLayout in root itemView or among itemView's children.");
  }

  /**
   * Binds the corresponding {@link ListItemLayout} to the adapter position by calling {@link
   * ListItemLayout#updateAppearance}.
   */
  public void bind() {
    int position = getBindingAdapterPosition();
    int itemCount = getBindingAdapter().getItemCount();
    bind(position, itemCount);
  }

  /**
   * Binds the corresponding {@link ListItemLayout} according given position and item count.
   * If there are several sections in the list, the position and item count given should be relative
   * to its section.
   */

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Make ListItemLayout the root of the item layout, or ensure it is a direct child of the itemView root (depth exactly one).
  2. Verify the adapter inflates the correct layout containing ListItemLayout when constructing ListItemViewHolder.
  3. If you need extra decoration containers, put them INSIDE the ListItemLayout, not around it.

Example fix

<!-- before: ListItemLayout nested too deep -->
<FrameLayout ...>
  <CardView ...>
    <ListItemLayout .../> <!-- depth 2: holder throws -->
  </CardView>
</FrameLayout>

<!-- after -->
<ListItemLayout ...>
  <CardView ...> ... content ... </CardView>
</ListItemLayout>
Defensive patterns

Strategy: validation

Validate before calling

View v = itemView instanceof ListItemLayout ? itemView
    : (((ViewGroup) itemView).getChildCount() > 0 ? ((ViewGroup) itemView).getChildAt(0) : null);
if (!(v instanceof ListItemLayout)) {
  throw new IllegalStateException("Item layout must have ListItemLayout as root or direct child");
}

Type guard

static boolean hasDirectListItemLayout(View itemView) {
  if (itemView instanceof ListItemLayout) return true;
  if (itemView instanceof ViewGroup) {
    ViewGroup g = (ViewGroup) itemView;
    for (int i = 0; i < g.getChildCount(); i++) {
      if (g.getChildAt(i) instanceof ListItemLayout) return true;
    }
  }
  return false;
}

Prevention

When it happens

Trigger: Creating a ListItemViewHolder with an item layout whose root is not ListItemLayout and which has no ListItemLayout as a DIRECT child (e.g. ListItemLayout nested two levels deep inside other containers); inflating the wrong layout resource for the holder.

Common situations: Wrapping the list item in an extra FrameLayout/CardView for decoration, pushing ListItemLayout below the first child level; layout refactors that rename or replace the ListItemLayout root; using a plain ViewHolder path with a generic item layout.

Related errors


AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14). Data as JSON: /api/errors/d64f1fdfbee254fb. Report an issue: GitHub.