DrKLO/Telegram · error · IllegalStateException

{positionDescription}: Could not instantiate the LayoutManag

Error message

{positionDescription}: Could not instantiate the LayoutManager: {className}

What it means

After resolving the LayoutManager class and a constructor, RecyclerView calls constructor.newInstance(...). If the constructor itself runs and THROWS, it is wrapped in an InvocationTargetException and surfaced as 'Could not instantiate the LayoutManager'. The cause chain holds the real exception from the constructor body.

Source

Thrown at TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java:874

                        constructor = layoutManagerClass
                                .getConstructor(LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE);
                        constructorArgs = new Object[]{context, attrs, defStyleAttr, defStyleRes};
                    } catch (NoSuchMethodException e) {
                        try {
                            constructor = layoutManagerClass.getConstructor();
                        } catch (NoSuchMethodException e1) {
                            e1.initCause(e);
                            throw new IllegalStateException(attrs.getPositionDescription()
                                    + ": Error creating LayoutManager " + className, e1);
                        }
                    }
                    constructor.setAccessible(true);
                    setLayoutManager(constructor.newInstance(constructorArgs));
                } catch (ClassNotFoundException e) {
                    throw new IllegalStateException(attrs.getPositionDescription()
                            + ": Unable to find LayoutManager " + className, e);
                } catch (InvocationTargetException e) {
                    throw new IllegalStateException(attrs.getPositionDescription()
                            + ": Could not instantiate the LayoutManager: " + className, e);
                } catch (InstantiationException e) {
                    throw new IllegalStateException(attrs.getPositionDescription()
                            + ": Could not instantiate the LayoutManager: " + className, e);
                } catch (IllegalAccessException e) {
                    throw new IllegalStateException(attrs.getPositionDescription()
                            + ": Cannot access non-public constructor " + className, e);
                } catch (ClassCastException e) {
                    throw new IllegalStateException(attrs.getPositionDescription()
                            + ": Class is not a LayoutManager " + className, e);
                }
            }
        }
    }

    private String getFullClassName(Context context, String className) {
        if (className.charAt(0) == '.') {
            return context.getPackageName() + className;

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Inspect e.getCause() of the thrown IllegalStateException — it holds the original failure from the constructor.
  2. For GridLayoutManager in XML, always provide a valid app:spanCount > 0.
  3. Make the custom LayoutManager constructor defensive: validate inputs and avoid work that can fail during inflation.

Example fix

<!-- before -->
<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>

<!-- after -->
<androidx.recyclerview.widget.RecyclerView
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="3"/>
Defensive patterns

Strategy: try-catch

Validate before calling

// For GridLayoutManager, supply a valid spanCount before/with the attribute
// e.g. ensure app:spanCount is set and > 0 in XML

Type guard

null

Try / catch

try {
    View v = inflater.inflate(R.layout.has_recycler, parent, false);
} catch (IllegalStateException e) {
    Throwable cause = e.getCause();
    // inspect cause (InvocationTargetException -> constructor failure) and fix config
    Log.e(TAG, "LayoutManager ctor failed", cause);
    throw e;
}

Prevention

When it happens

Trigger: The LayoutManager constructor throws — e.g. GridLayoutManager(Context, AttributeSet, int, int) throws when a spanCount XML attribute is 0 or negative, or a custom constructor does null-deref / illegal state work.

Common situations: Declaring a GridLayoutManager in XML without app:spanCount (defaults may be 0 -> IllegalArgumentException inside constructor). Custom LayoutManager constructor performs I/O or context lookups that fail. Version mismatch where a constructor expects attributes that are absent.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/90b2c9ec1bdbc6de. Report an issue: GitHub.