DrKLO/Telegram · error · IllegalStateException

{positionDescription}: Cannot access non-public constructor

Error message

{positionDescription}: Cannot access non-public constructor {className}

What it means

constructor.setAccessible(true) is called, but if newInstance(...) still raises IllegalAccessException, RecyclerView reports 'Cannot access non-public constructor'. This indicates the constructor (or its declaring class) is inaccessible to the caller even after setAccessible, e.g. due to a SecurityManager or module boundaries.

Source

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

                        } 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;
        }
        if (className.contains(".")) {
            return className;
        }
        return RecyclerView.class.getPackage().getName() + '.' + className;
    }

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Make the LayoutManager class and at least one supported constructor public.
  2. Avoid referencing inner non-static or private classes as a layoutManager.
  3. Instantiate the LayoutManager programmatically via setLayoutManager(new MyManager(...)) instead of through XML reflection.

Example fix

// before
class MyManager : LinearLayoutManager(context) { // package-private class
    private constructor(ctx: Context)
}

// after
class MyManager(ctx: Context) : LinearLayoutManager(ctx) // public class + public ctor
Defensive patterns

Strategy: validation

Validate before calling

static boolean isPublicInstantiable(Class<?> c) {
    int mod = c.getModifiers();
    if (!Modifier.isPublic(mod)) return false;
    for (java.lang.reflect.Constructor<?> ctor : c.getConstructors()) {
        if (ctor.getParameterTypes().length == 0) return true;
    }
    return false;
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: LayoutManager constructor is private/protected and the runtime enforces access checks that setAccessible cannot bypass (older API levels, SecurityManager present, or a non-public class). Constructor exists but the declaring class itself is non-public.

Common situations: Kotlin object/singletons or private-constructored classes referenced from XML. Class is package-private and inflation crosses a package boundary. Embedded environments with a SecurityManager that vetoes setAccessible.

Related errors


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