material-components/material-components-android · error · IllegalStateException
Unable to create badge
Error message
Unable to create badge
What it means
TabView.getOrCreateBadge() lazily creates a BadgeDrawable (BadgeDrawable.create(context)) and anchors it; the code then re-checks badgeDrawable != null and throws IllegalStateException('Unable to create badge') if creation still yielded null. This is a defensive assertion: BadgeDrawable.create is not expected to return null, so hitting it means badge creation was unexpectedly impossible in this environment.
Source
Thrown at lib/java/com/google/android/material/tabs/TabLayout.java:2867
.inflate(R.layout.design_layout_tab_text, this, false);
addView(textView);
}
/**
* Creates an instance of {@link BadgeDrawable} if none exists. Initializes (if needed) and
* returns the associated instance of {@link BadgeDrawable}.
*
* @return an instance of BadgeDrawable associated with {@code Tab}.
*/
@NonNull
private BadgeDrawable getOrCreateBadge() {
// Creates a new instance if one is not already initialized for this TabView.
if (badgeDrawable == null) {
badgeDrawable = BadgeDrawable.create(getContext());
}
tryUpdateBadgeAnchor();
if (badgeDrawable == null) {
throw new IllegalStateException("Unable to create badge");
}
return badgeDrawable;
}
@Nullable
private BadgeDrawable getBadge() {
return badgeDrawable;
}
private void removeBadge() {
if (badgeAnchorView != null) {
tryRemoveBadgeFromAnchor();
}
badgeDrawable = null;
}
private void addOnLayoutChangeListener(@Nullable final View view) {
if (view == null) {View on GitHub (pinned to ac7e18efee)
Solutions
- Ensure the stock com.google.android.material dependency is used (no forks shadowing BadgeDrawable).
- If mocking in tests, do not stub BadgeDrawable.create to return null (use real instances or returnsSmartNulls).
- Report upstream if it reproduces with an unmodified library — it indicates a Material internals defect.
Defensive patterns
Strategy: try-catch
Try / catch
try {
BadgeDrawable badge = tab.getOrCreateBadge();
badge.setVisible(true);
} catch (IllegalStateException e) {
// Badge creation unexpectedly failed; degrade gracefully (no badge) and log.
Log.w(TAG, "Badge unavailable for tab", e);
} Prevention
- Do not fork or shadow BadgeDrawable.create.
- In unit tests, avoid mocking that stubs BadgeDrawable.create to null.
- Call getOrCreateBadge lazily right before configuring the badge.
When it happens
Trigger: Calling tab.getOrCreateBadge() (or getBadge/tryUpdateBadgeAnchor paths that route through it) in an environment where BadgeDrawable.create returns null — effectively unreachable in stock Material versions; realistic only with modified/overridden badge creation or unusual runtime instrumentation.
Common situations: Forks or shadows of the Material library where BadgeDrawable.create was altered; test environments/mocking frameworks that stub BadgeDrawable.create to return null.
Related errors
- Tab belongs to a different TabLayout.
- Tab does not belong to this TabLayout.
- is not a valid TabIndicatorAnimationMode
- Only TabItem instances can be added to TabLayout
- Tab not attached to a TabLayout
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/bc796a89970a5871.
Report an issue: GitHub.