Blankj/AndroidUtilCode · error · IllegalArgumentException
no menus
Error message
no menus
What it means
SwipeRightMenu is a horizontal LinearLayout: the child at index 0 is the main content and every child at index >= 1 is a swipe-revealed menu action. initView() (scheduled via post(), so it runs after the view is attached) reads getChildCount() and throws IllegalArgumentException("no menus") when it is <= 1, i.e. there is content but no menu children to reveal.
Source
Thrown at lib/utildebug/src/main/java/com/blankj/utildebug/base/view/SwipeRightMenu.java:61
public SwipeRightMenu(Context context) {
this(context, null);
}
public SwipeRightMenu(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setOrientation(HORIZONTAL);
post(new Runnable() {
@Override
public void run() {
initView();
}
});
}
private void initView() {
int childCount = getChildCount();
if (childCount <= 1) {
throw new IllegalArgumentException("no menus");
}
mContentView = getChildAt(0);
for (int i = 1; i < childCount; i++) {
MenuBean bean = new MenuBean(getChildAt(i));
mMenus.add(bean);
if (i == 1) {
bean.setCloseMargin(0);
} else {
bean.setCloseMargin(-mMenus.get(i - 2).getWidth());
}
mMenusWidth += bean.getWidth();
}
for (int i = 0; i < mMenus.size(); i++) {
mMenus.get(i).setOpenMargin(0);
}
}
View on GitHub (pinned to 7b4caf9e54)
Solutions
- Add at least one extra child view to SwipeRightMenu in XML (or before attach): index 0 is content, index 1+ are the menus, so getChildCount() must be >= 2.
- If menus are added dynamically, add them before the view is attached/laid out so they exist when the posted initView() callback executes.
- After setContentView/inflation, assert getChildCount() >= 2 to fail with a clearer message than the library's.
Example fix
// before
<com.blankj.utildebug.base.view.SwipeRightMenu
android:id="@+id/swipeMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Content" />
<!-- only content, no menu child -> throws "no menus" -->
</com.blankj.utildebug.base.view.SwipeRightMenu>
// after
<com.blankj.utildebug.base.view.SwipeRightMenu ...>
<TextView ... android:text="Content" />
<TextView ... android:text="Delete" /> <!-- menu child at index 1 -->
</com.blankj.utildebug.base.view.SwipeRightMenu> Defensive patterns
Strategy: validation
Validate before calling
ViewGroup menu = findViewById(R.id.swipeMenu);
if (menu.getChildCount() < 2) {
throw new IllegalStateException(
"SwipeRightMenu needs >=1 menu child besides content; found "
+ (menu.getChildCount() - 1));
} Type guard
private static boolean hasSwipeMenus(ViewGroup v) {
return v != null && v.getChildCount() > 1;
} Prevention
- Always pair the content child with at least one menu-action child inside SwipeRightMenu.
- When building the menu in code, add menu views before the view is attached so the post() callback sees them.
- Treat getChildCount() < 2 as a programmer error in your own layout, not a runtime condition to swallow.
When it happens
Trigger: Inflating or constructing SwipeRightMenu with only a single child view (the content) and no additional menu-action views; or adding the view programmatically with just one child so that getChildCount() == 1 when the posted initView() runs.
Common situations: Layout XML places only the content view inside <SwipeRightMenu> and forgets the menu buttons; menu items are added in code but after the post() runnable already fired; a copy of the custom view omits the menu children.
Related errors
- ContentView is null.
- context is not instance of Activity.
- presenterClass is null!
- presenter of <{}> is not added!
- onCreateViewHolder: get holder from view type failed.
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/1937c5126b4f67e3.
Report an issue: GitHub.