DrKLO/Telegram · error · IllegalStateException

{positionDescription}: Error creating LayoutManager {classNa

Error message

{positionDescription}: Error creating LayoutManager {className}

What it means

When a LayoutManager class name is declared in XML (app:layoutManager), RecyclerView reflects to instantiate it. It first tries the (Context, AttributeSet, int, int) constructor; on NoSuchMethodException it falls back to the no-arg constructor. If NEITHER exists, it throws 'Error creating LayoutManager' — the class exists but lacks an invokable constructor.

Source

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

                        classLoader = this.getClass().getClassLoader();
                    } else {
                        classLoader = context.getClassLoader();
                    }
                    Class<? extends LayoutManager> layoutManagerClass =
                            Class.forName(className, false, classLoader)
                                    .asSubclass(LayoutManager.class);
                    Constructor<? extends LayoutManager> constructor;
                    Object[] constructorArgs = null;
                    try {
                        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) {

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Add a public constructor matching LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE (Context, AttributeSet, int, int) to your LayoutManager subclass.
  2. Or add a public no-arg constructor as the fallback.
  3. Ensure the class and its constructor are not stripped by ProGuard (add a keep rule for LayoutManager subclasses referenced in XML).

Example fix

// before
public class MyGridManager extends GridLayoutManager {
    public MyGridManager(Context ctx, int span) { super(ctx, span); }
}

// after
public class MyGridManager extends GridLayoutManager {
    public MyGridManager() { super(/* defaults */); }
    public MyGridManager(Context ctx, int span) { super(ctx, span); }
}
Defensive patterns

Strategy: validation

Validate before calling

// Verify the LayoutManager subclass has a usable constructor at build time
static boolean hasUsableCtor(Class<?> c) throws NoSuchMethodException {
    try {
        c.getConstructor(Context.class, AttributeSet.class, int.class, int.class);
        return true;
    } catch (NoSuchMethodException e) {
        c.getConstructor();
        return true;
    }
}

Type guard

static boolean isInstantiableLayoutManager(Class<?> c) {
    return RecyclerView.LayoutManager.class.isAssignableFrom(c)
        && !Modifier.isAbstract(c.getModifiers())
        && !c.isInterface();
}

Try / catch

null

Prevention

When it happens

Trigger: Declaring app:layoutManager="com.example.MyGridManager" in layout XML where MyGridManager only exposes a constructor like (Context, int) or (int) and has no public default constructor nor the 4-arg signature.

Common situations: Custom LayoutManager written with only a (Context) or (int, int) constructor. Constructor was made private for a builder pattern. ProGuard/R8 stripped or renamed a constructor that the reflection path expects.

Related errors


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