DrKLO/Telegram · error · IllegalStateException
Cannot call this method unless RecyclerView is computing a l
Error message
Cannot call this method unless RecyclerView is computing a layout or scrolling
What it means
assertInLayoutOrScroll(null) is the guard used by methods that may ONLY run while RecyclerView is computing a layout or scrolling (e.g. adapter notify calls inside onLayoutChildren, view measurement helpers). If isComputingLayout() is false and no custom message was supplied, it throws the generic 'Cannot call this method unless RecyclerView is computing a layout or scrolling'. The call must happen inside the layout/scroll window.
Source
Thrown at TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java:3039
/**
* Returns true if RecyclerView is attached to window.
*/
@Override
public boolean isAttachedToWindow() {
return mIsAttached;
}
/**
* Checks if RecyclerView is in the middle of a layout or scroll and throws an
* {@link IllegalStateException} if it <b>is not</b>.
*
* @param message The message for the exception. Can be null.
* @see #assertNotInLayoutOrScroll(String)
*/
void assertInLayoutOrScroll(String message) {
if (!isComputingLayout()) {
if (message == null) {
throw new IllegalStateException("Cannot call this method unless RecyclerView is "
+ "computing a layout or scrolling" + exceptionLabel());
}
throw new IllegalStateException(message + exceptionLabel());
}
}
/**
* Checks if RecyclerView is in the middle of a layout or scroll and throws an
* {@link IllegalStateException} if it <b>is</b>.
*
* @param message The message for the exception. Can be null.
* @see #assertInLayoutOrScroll(String)
*/
void assertNotInLayoutOrScroll(String message) {
if (isComputingLayout()) {
if (message == null) {
throw new IllegalStateException("Cannot call this method while RecyclerView is "View on GitHub (pinned to 45ab8f4308)
Solutions
- Move the call inside onLayoutChildren / onScrollChanged / a layout-pass callback where isComputingLayout() is true.
- If you need the result after layout, post the call to the next frame via recyclerView.post(...) once layout is done.
- Guard with: if (recyclerView.isComputingLayout()) { ... } else { postpone; }.
Example fix
// before
View v = layoutManager.findViewByPosition(pos); // called from onClick
// after
recyclerView.post(() -> {
View v = layoutManager.findViewByPosition(pos);
}); Defensive patterns
Strategy: validation
Validate before calling
if (recyclerView.isComputingLayout()) {
// safe to call layout-time-only APIs
doLayoutTimeWork();
} else {
recyclerView.post(this::doLayoutTimeWork);
} Type guard
null
Try / catch
null
Prevention
- Check isComputingLayout() before calling layout-time-only helpers.
- Post deferred work to the next frame when not in a layout pass.
- Avoid invoking LayoutManager helpers from click handlers and async callbacks directly.
When it happens
Trigger: Calling a layout-time-only API (e.g. layoutManager.findViewByPosition, certain offsetDescendants methods, scrap interactions) from outside onLayout/onScroll — e.g. from a click handler, an async callback, or onResume before the first layout pass.
Common situations: Calling notifyDataSetChanged or scroll-dependent lookups from a coroutine/Handler post that races ahead of layout. Invoking LayoutManager helper methods during initialization before the RecyclerView is attached.
Related errors
- {message}
- Cannot call this method while RecyclerView is computing a la
- should not dispatch add or move for pre layout
- op should be remove or update.{}
- Called attach on a child which is not detached: {vh}
AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14).
Data as JSON: /api/errors/5d87746f39d7df95.
Report an issue: GitHub.