{"record":{"id":"2528a5f421087aa1","repo":"AvaloniaUI/Avalonia","slug":"the-control-isn-t-currently-attached-to-a-toplevel","errorCode":null,"errorMessage":"The control isn't currently attached to a toplevel","messagePattern":"The control isn't currently attached to a toplevel","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Android/Avalonia.Android/Platform/AndroidNativeControlHostImpl.cs","lineNumber":124,"sourceCode":"            public bool IsCompatibleWith(INativeControlHostImpl host) => host is AndroidNativeControlHostImpl;\n\n            public void HideWithSize(Size size)\n            {\n                CheckDisposed();\n                if (_attachedTo == null)\n                    return;\n\n                size *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;\n                _view.Visibility = ViewStates.Gone;\n                _view.LayoutParameters = new FrameLayout.LayoutParams(Math.Max(1, (int)size.Width), Math.Max(1, (int)size.Height));\n                _view.RequestLayout();\n            }\n\n            public void ShowInBounds(Rect bounds)\n            {\n                CheckDisposed();\n                if (_attachedTo == null)\n                    throw new InvalidOperationException(\"The control isn't currently attached to a toplevel\");\n\n                bounds *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;\n                _view.Visibility = ViewStates.Visible;\n                _view.LayoutParameters = new FrameLayout.LayoutParams(Math.Max(1, (int)bounds.Width), Math.Max(1, (int)bounds.Height))\n                {\n                    LeftMargin = (int)bounds.X,\n                    TopMargin = (int)bounds.Y\n                };\n                _view.RequestLayout();\n            }\n        }\n    }\n}\n","sourceCodeStart":106,"sourceCodeEnd":138,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Android/Avalonia.Android/Platform/AndroidNativeControlHostImpl.cs#L106-L138","documentation":"AndroidNativeControlHostImpl's per-control holder requires _attachedTo (the parent TopLevel host) to be set before ShowInBounds places a native child view. If a child is asked to show before/after attach (or after detach), _attachedTo is null and it throws InvalidOperationException.","triggerScenarios":"Calling ShowInBounds (or any layout call) on a hosted native Android child view while the control host is not attached to a TopLevel — before Attach or after Detach, or when the TopLevel was torn down.","commonSituations":"Triggering native control layout during early init before the host is attached; re-laying out controls after the window closed; controls reparented at runtime; calling ShowInBounds from a detached control host.","solutions":["Only show/layout native children after the control host is attached to a TopLevel (e.g. in AttachedToVisualTree).","Detach/stop layout when the host detaches so no ShowInBounds runs post-detach.","Guard with `if (_attachedTo is null) return;` at call sites during transition windows.","Audit control lifetime so children are not measured while detached."],"exampleFix":"// before\npublic void ShowInBounds(Rect bounds)\n{\n    CheckDisposed();\n    if (_attachedTo == null)\n        throw new InvalidOperationException(\"The control isn't currently attached to a toplevel\");\n    bounds *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;\n    ...\n}\n\n// after (no-op until attached; attach/detach driven by host lifecycle)\npublic void ShowInBounds(Rect bounds)\n{\n    CheckDisposed();\n    if (_attachedTo is null) return; // not attached yet — layout will run on attach\n    bounds *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;\n    ...\n}","handlingStrategy":"validation","validationCode":"// only show children when attached\nif (_attachedTo is null) return;","typeGuard":"static bool IsAttached<TH>(TH host) where TH : class => host is not null;","tryCatchPattern":"// guard call sites instead of catching; drive ShowInBounds off attach/detach.","preventionTips":["Lay out native children only while attached to a TopLevel.","No-op ShowInBounds when _attachedTo is null.","Detach children when the host detaches."],"tags":["android","native-control-host","toplevel","lifecycle","layout","platform"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}