material-components/material-components-android · error · IllegalArgumentException
Invalid swipe state: {}
Error message
Invalid swipe state: {} What it means
The gravity-based setSwipeState only accepts the three stable swipe states (STATE_CLOSED, STATE_OPEN, STATE_SWIPE_PRIMARY_ACTION). The @StableSwipeState annotation documents this at compile time; at runtime an unrecognized value throws IllegalArgumentException with the value printed, before any layout work starts.
Source
Thrown at lib/java/com/google/android/material/listitem/ListItemLayout.java:917
public void setSwipeState(@StableSwipeState int swipeState, @RevealGravity int revealGravity) {
setSwipeState(swipeState, revealGravity, /* animate= */ true);
}
/**
* Sets the state to swipe the {@link SwipeableListItem} child to.
*
* @param swipeState The state to swipe to. This must be one of {@link
* SwipeableListItem#STATE_CLOSED}, {@link SwipeableListItem#STATE_OPEN}, or {@link
* SwipeableListItem#STATE_SWIPE_PRIMARY_ACTION}
* @param revealGravity The gravity of the {@link RevealableListItem} to reveal when swiping.
* @param animate Whether to animate to the given swipe state.
*/
public void setSwipeState(
@StableSwipeState int swipeState, @RevealGravity int revealGravity, boolean animate) {
if (swipeState != STATE_CLOSED
&& swipeState != STATE_OPEN
&& swipeState != STATE_SWIPE_PRIMARY_ACTION) {
throw new IllegalArgumentException("Invalid swipe state: " + swipeState);
} else if (!(contentView instanceof SwipeableListItem) || !swipeToRevealLayoutExists()) {
throw new IllegalArgumentException(
"ListItemLayout must have a SwipeableListItem child and a RevealableListItem child to be"
+ " swiped.");
} else if (swipeState != STATE_CLOSED && !swipeToRevealLayoutExistsForGravity(revealGravity)) {
throw new IllegalArgumentException(
"No RevealableListItem is defined for the given gravity: " + revealGravity);
}
Runnable runnable =
() -> {
if (!animate) {
if (viewDragHelper != null) {
viewDragHelper.abort();
}
int finalLeft = getOffsetForSwipeState(swipeState, revealGravity);
contentView.offsetLeftAndRight(finalLeft - contentView.getLeft());
updateSwipeProgress(finalLeft);View on GitHub (pinned to ac7e18efee)
Solutions
- Whitelist incoming values: validate the stored state is one of the three constants before calling setSwipeState, defaulting to STATE_CLOSED.
- Always reference SwipeableListItem.STATE_* constants instead of literal ints.
- Annotate your own parameters with @StableSwipeState so IDE lint flags invalid values.
Example fix
// before
listItemLayout.setSwipeState(intentExtraState, gravity, true); // unvalidated
// after
int state = (extra == SwipeableListItem.STATE_OPEN
|| extra == SwipeableListItem.STATE_SWIPE_PRIMARY_ACTION)
? extra : SwipeableListItem.STATE_CLOSED;
listItemLayout.setSwipeState(state, gravity, true); Defensive patterns
Strategy: validation
Validate before calling
int safe = (state == SwipeableListItem.STATE_OPEN
|| state == SwipeableListItem.STATE_SWIPE_PRIMARY_ACTION)
? state : SwipeableListItem.STATE_CLOSED;
listItemLayout.setSwipeState(safe, revealGravity, animate); Type guard
static boolean isStableSwipeState(int s) {
return s == SwipeableListItem.STATE_CLOSED
|| s == SwipeableListItem.STATE_OPEN
|| s == SwipeableListItem.STATE_SWIPE_PRIMARY_ACTION;
} Prevention
- Whitelist persisted/intent-supplied states against the three constants.
- Keep a single source of truth for swipe-state constants in your app.
- Add unit tests covering unknown-state fallback behavior.
When it happens
Trigger: Passing an arbitrary/unknown int as swipeState (e.g. from a bundled intent extra or persisted preference without validation); using constants copied from a different swipe library; typos in mirrored constants after a library update.
Common situations: Restoring swipe state from savedInstanceState or a database; receiving desired state from deep links; code shared across app modules with divergent constant definitions.
Related errors
- Only one RevealableListItem with end gravity is supported.
- Only one RevealableListItem with start gravity is supported.
- Only one SwipeableListItem view is allowed in a ListItemLayo
- No RevealableListItem with gravity {}
- Invalid state to get swipe offset: {}
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/37c0066258e540b7.
Report an issue: GitHub.