{"record":{"id":"2da10bda401cc665","repo":"AvaloniaUI/Avalonia","slug":"avaloniaview-context-must-not-be-null","errorCode":null,"errorMessage":"AvaloniaView.Context must not be null","messagePattern":"AvaloniaView\\.Context must not be null","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"critical","filePath":"src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs","lineNumber":51,"sourceCode":"        private readonly AndroidKeyboardEventsHelper<TopLevelImpl> _keyboardHelper;\n        private readonly AndroidMotionEventsHelper _pointerHelper;\n        private readonly AndroidInputMethod<AvaloniaView> _textInputMethod;\n        private readonly INativeControlHostImpl _nativeControlHost;\n        private readonly IStorageProvider? _storageProvider;\n        private readonly AndroidSystemNavigationManagerImpl _systemNavigationManager;\n        private readonly AndroidInsetsManager? _insetsManager;\n        private readonly Clipboard _clipboard;\n        private readonly AndroidLauncher? _launcher;\n        private readonly AndroidScreens? _screens;\n        private readonly AndroidPlatformFeedback _feedback;\n        private SurfaceViewImpl? _view;\n        private WindowTransparencyLevel _transparencyLevel;\n\n        public TopLevelImpl(AvaloniaView avaloniaView, bool placeOnTop = false)\n        {\n            if (avaloniaView.Context is not { } context)\n            {\n                throw new ArgumentException(\"AvaloniaView.Context must not be null\");\n            }\n\n            _view = new SurfaceViewImpl(context, this, placeOnTop);\n            _textInputMethod = new AndroidInputMethod<AvaloniaView>(avaloniaView);\n            _keyboardHelper = new AndroidKeyboardEventsHelper<TopLevelImpl>(this);\n            _pointerHelper = new AndroidMotionEventsHelper(this);\n            _clipboard = new Clipboard(new ClipboardImpl(\n                context.GetSystemService(Context.ClipboardService).JavaCast<ClipboardManager>(),\n                context));\n            _screens = new AndroidScreens(context);\n            _feedback = new AndroidPlatformFeedback(avaloniaView);\n\n            if (context is Activity mainActivity)\n            {\n                _insetsManager = new AndroidInsetsManager(mainActivity, this);\n                _storageProvider = new AndroidStorageProvider(mainActivity);\n                _launcher = new AndroidLauncher(mainActivity);\n            }","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs#L33-L69","documentation":"Thrown in the TopLevelImpl constructor when the passed AvaloniaView has a null Context. TopLevelImpl immediately needs the Context to create the SurfaceViewImpl, obtain system services (ClipboardManager), and wire up platform helpers — a null Context makes all of that impossible.","triggerScenarios":"Constructing TopLevelImpl with an AvaloniaView instance whose Context property returns null. This typically means the AvaloniaView was created programmatically with a null context, or it was already detached from its window when TopLevelImpl was instantiated.","commonSituations":"Programmatically new-ing AvaloniaView(null) or with a detached view; constructing TopLevelImpl in a background thread after the view was detached; misuse in custom embedding code that constructs views without an Activity context; tests that pass a partially-initialized AvaloniaView.","solutions":["Always create AvaloniaView with a valid Context (the host Activity): new AvaloniaView(activity).","Defer TopLevelImpl construction until the AvaloniaView is attached (Context is non-null); construct it inside onAttachedToWindow or after addToWindow.","If the view can be detached, guard any TopLevelImpl creation with a null check on view.Context.","In tests, supply a real or Robolectric context to the AvaloniaView."],"exampleFix":"// before\nvar avaloniaView = new AvaloniaView(); // Context may be null\nvar topLevel = new TopLevelImpl(avaloniaView);\n\n// after\nvar avaloniaView = new AvaloniaView(activity);\nvar topLevel = new TopLevelImpl(avaloniaView);","handlingStrategy":"validation","validationCode":"// construct AvaloniaView with a valid Context, and verify before TopLevelImpl\nif (avaloniaView.Context is null)\n    throw new InvalidOperationException(\"AvaloniaView must have a non-null Context.\");\nvar topLevel = new TopLevelImpl(avaloniaView);","typeGuard":"static bool HasContext(AvaloniaView v) => v.Context is not null;","tryCatchPattern":null,"preventionTips":["Always create AvaloniaView with a valid Activity: new AvaloniaView(activity).","Defer TopLevelImpl creation until the view is attached (Context non-null).","Null-check view.Context in custom embedding code.","In tests, supply a real or Robolectric context."],"tags":["android","context","construction","toplevel"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}