DrKLO/Telegram · error · IllegalStateException
RecyclerView has no LayoutManager
Error message
RecyclerView has no LayoutManager
What it means
RecyclerView.generateDefaultLayoutParams throws IllegalStateException when mLayout (the LayoutManager) is null. generateDefaultLayoutParams is invoked by the ViewGroup framework when a child view added to the RecyclerView has no LayoutParams. A RecyclerView without a LayoutManager cannot produce valid LayoutParams (they are LayoutManager-specific), so the framework aborts rather than returning default ViewGroup params that would crash later during measurement.
Source
Thrown at TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java:4519
@Override
public void onDraw(Canvas c) {
super.onDraw(c);
final int count = mItemDecorations.size();
for (int i = 0; i < count; i++) {
mItemDecorations.get(i).onDraw(c, this, mState);
}
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams && mLayout.checkLayoutParams((LayoutParams) p);
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
if (mLayout == null) {
throw new IllegalStateException("RecyclerView has no LayoutManager" + exceptionLabel());
}
return mLayout.generateDefaultLayoutParams();
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
if (mLayout == null) {
throw new IllegalStateException("RecyclerView has no LayoutManager" + exceptionLabel());
}
return mLayout.generateLayoutParams(getContext(), attrs);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
if (mLayout == null) {
throw new IllegalStateException("RecyclerView has no LayoutManager" + exceptionLabel());
}
return mLayout.generateLayoutParams(p);View on GitHub (pinned to 45ab8f4308)
Solutions
- Call recyclerView.setLayoutManager(...) before adding any child views or binding adapter content — typically immediately after instantiation.
- Never declare RecyclerView children in XML; RecyclerView is a container whose children come exclusively from the adapter via the LayoutManager.
- If clearing the manager, ensure no child views remain attached before the next measure pass (recyclerView.removeAllViews() then setLayoutManager(null)).
Example fix
// before RecyclerView rv = new RecyclerView(ctx); rv.addView(customHeader); // throws: no LayoutManager rv.setLayoutManager(new LinearLayoutManager(ctx)); // after RecyclerView rv = new RecyclerView(ctx); rv.setLayoutManager(new LinearLayoutManager(ctx)); rv.addView(customHeader); // ok (though adapter is the normal path)
Defensive patterns
Strategy: validation
Validate before calling
if (recyclerView.getLayoutManager() == null) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
}
recyclerView.addView(child); Prevention
- Set the LayoutManager immediately after constructing the RecyclerView (code or XML app:layoutManager).
- Supply children only via the adapter, never via addView or XML declarations inside <RecyclerView>.
When it happens
Trigger: Adding a child view (directly via addView, or via inflate that parents into the RecyclerView) before a LayoutManager has been set via setLayoutManager(); instantiating RecyclerView programmatically and adding children before setLayoutManager; inflating a layout into the RecyclerView before assigning the manager.
Common situations: Dynamic RecyclerView creation in code where setLayoutManager is called after views are inserted; layout XML that declares RecyclerView children (RecyclerView children should be supplied by the adapter, never declared in XML); programmatic re-parenting of a view into a RecyclerView whose manager was cleared.
Related errors
- {positionDescription}: Could not instantiate the LayoutManag
- view is not a child, cannot hide {}
- Invalid orientation. It should be either HORIZONTAL or VERTI
- Layout positions must be non-negative
- Pixel distance must be non-negative
AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14).
Data as JSON: /api/errors/82a19c2e6a02147c.
Report an issue: GitHub.