AvaloniaUI/Avalonia · critical · ArgumentException

AvaloniaView.Context must not be null

Error message

AvaloniaView.Context must not be null

What it means

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.

Source

Thrown at src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs:51

        private readonly AndroidKeyboardEventsHelper<TopLevelImpl> _keyboardHelper;
        private readonly AndroidMotionEventsHelper _pointerHelper;
        private readonly AndroidInputMethod<AvaloniaView> _textInputMethod;
        private readonly INativeControlHostImpl _nativeControlHost;
        private readonly IStorageProvider? _storageProvider;
        private readonly AndroidSystemNavigationManagerImpl _systemNavigationManager;
        private readonly AndroidInsetsManager? _insetsManager;
        private readonly Clipboard _clipboard;
        private readonly AndroidLauncher? _launcher;
        private readonly AndroidScreens? _screens;
        private readonly AndroidPlatformFeedback _feedback;
        private SurfaceViewImpl? _view;
        private WindowTransparencyLevel _transparencyLevel;

        public TopLevelImpl(AvaloniaView avaloniaView, bool placeOnTop = false)
        {
            if (avaloniaView.Context is not { } context)
            {
                throw new ArgumentException("AvaloniaView.Context must not be null");
            }

            _view = new SurfaceViewImpl(context, this, placeOnTop);
            _textInputMethod = new AndroidInputMethod<AvaloniaView>(avaloniaView);
            _keyboardHelper = new AndroidKeyboardEventsHelper<TopLevelImpl>(this);
            _pointerHelper = new AndroidMotionEventsHelper(this);
            _clipboard = new Clipboard(new ClipboardImpl(
                context.GetSystemService(Context.ClipboardService).JavaCast<ClipboardManager>(),
                context));
            _screens = new AndroidScreens(context);
            _feedback = new AndroidPlatformFeedback(avaloniaView);

            if (context is Activity mainActivity)
            {
                _insetsManager = new AndroidInsetsManager(mainActivity, this);
                _storageProvider = new AndroidStorageProvider(mainActivity);
                _launcher = new AndroidLauncher(mainActivity);
            }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Always create AvaloniaView with a valid Context (the host Activity): new AvaloniaView(activity).
  2. Defer TopLevelImpl construction until the AvaloniaView is attached (Context is non-null); construct it inside onAttachedToWindow or after addToWindow.
  3. If the view can be detached, guard any TopLevelImpl creation with a null check on view.Context.
  4. In tests, supply a real or Robolectric context to the AvaloniaView.

Example fix

// before
var avaloniaView = new AvaloniaView(); // Context may be null
var topLevel = new TopLevelImpl(avaloniaView);

// after
var avaloniaView = new AvaloniaView(activity);
var topLevel = new TopLevelImpl(avaloniaView);
Defensive patterns

Strategy: validation

Validate before calling

// construct AvaloniaView with a valid Context, and verify before TopLevelImpl
if (avaloniaView.Context is null)
    throw new InvalidOperationException("AvaloniaView must have a non-null Context.");
var topLevel = new TopLevelImpl(avaloniaView);

Type guard

static bool HasContext(AvaloniaView v) => v.Context is not null;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/2da10bda401cc665. Report an issue: GitHub.