material-components/material-components-android · error · IllegalArgumentException

Unable to find anchor view with id:

Error message

Unable to find anchor view with id: 

What it means

IllegalArgumentException from setAnchorView(int) when no view with the given ID exists inside targetParent. The convenience overload resolves the ID via targetParent.findViewById and cannot fall back, so a typo'd ID, a different layout for the current configuration, or a not-yet-inflated hierarchy all throw immediately.

Source

Thrown at lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java:628

  @NonNull
  public B setAnchorView(@Nullable View anchorView) {
    if (this.anchor != null) {
      this.anchor.unanchor();
    }
    this.anchor = anchorView == null ? null : Anchor.anchor(this, anchorView);
    return (B) this;
  }

  /**
   * Sets the view the {@link BaseTransientBottomBar} should be anchored above by id.
   *
   * @throws IllegalArgumentException if the anchor view is not found.
   */
  @NonNull
  public B setAnchorView(@IdRes int anchorViewId) {
    View anchorView = targetParent.findViewById(anchorViewId);
    if (anchorView == null) {
      throw new IllegalArgumentException("Unable to find anchor view with id: " + anchorViewId);
    }
    return setAnchorView(anchorView);
  }

  /**
   * Returns whether the anchor view layout listener is enabled.
   *
   * @see #setAnchorViewLayoutListenerEnabled(boolean)
   */
  public boolean isAnchorViewLayoutListenerEnabled() {
    return anchorViewLayoutListenerEnabled;
  }

  /**
   * Sets whether the anchor view layout listener is enabled. If enabled, the {@link
   * BaseTransientBottomBar} will recalculate and update its position when the position of the
   * anchor view is changed.
   */

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Resolve the view yourself and null-check: View v = parent.findViewById(id); if (v != null) bar.setAnchorView(v);
  2. Ensure the ID exists in every layout variant used with this screen.
  3. Call setAnchorView only after the view hierarchy is fully inflated (e.g. onViewCreated / post).
  4. Remove the anchor (setAnchorView(null)) gracefully when the view is absent.

Example fix

// before
snackbar.setAnchorView(R.id.fab); // throws when R.id.fab absent in this layout

// after
View fab = findViewById(R.id.fab);
if (fab != null) snackbar.setAnchorView(fab);
snackbar.show();
Defensive patterns

Strategy: validation

Validate before calling

View anchor = parentView.findViewById(R.id.fab);
if (anchor != null) {
  snackbar.setAnchorView(anchor);
}
snackbar.show();

Prevention

When it happens

Trigger: setAnchorView(R.id.fab) where R.id.fab is in a different layout (landscape/tablet variant) or the fragment's view is not the targetParent's subtree; calling it before setContentView/onCreateView finished.

Common situations: Layout variants (layout-land, sw600dp) that omit the anchored view; anchoring to a view inside a different fragment; proguard/ID confusion rarely, but mostly plain ID typos or refactors renaming the view.

Related errors


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