AvaloniaUI/Avalonia · critical · InvalidOperationException
Avalonia Application was not initialized. Make sure you have
Error message
Avalonia Application was not initialized. Make sure you have created AvaloniaMainActivity.
What it means
InitializeAvaloniaView creates the backing AvaloniaView but first requires the Avalonia Application to exist (Avalonia.Application.Current). A null Current means no Avalonia app instance was built/started yet — typically because the host did not set up an AvaloniaMainActivity that runs AppBuilder. The view cannot be created without the application.
Source
Thrown at src/Android/Avalonia.Android/AvaloniaActivity.cs:209
base.OnActivityResult(requestCode, resultCode, data);
ActivityResult?.Invoke(requestCode, resultCode, data);
}
[SupportedOSPlatform("android23.0")]
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
RequestPermissionsResult?.Invoke(requestCode, permissions, grantResults);
}
[MemberNotNull(nameof(_view))]
private protected virtual void InitializeAvaloniaView(object? initialContent)
{
if (Avalonia.Application.Current is null)
{
throw new InvalidOperationException(
"Avalonia Application was not initialized. Make sure you have created AvaloniaMainActivity.");
}
_view = new AvaloniaView(this) { Content = initialContent };
}
private void HandleIntent(Intent? intent)
{
if (intent?.Data is { } androidUri
&& androidUri.IsAbsolute
&& Uri.TryCreate(androidUri.ToString(), UriKind.Absolute, out var uri))
{
if (uri.Scheme == Uri.UriSchemeFile || uri.Scheme == "content")
{
if (AndroidStorageItem.CreateItem(this, androidUri) is { } item)
{
_onActivated?.Invoke(this, new FileActivatedEventArgs(new[] { item }));
}View on GitHub (pinned to 11c5427268)
Solutions
- Create an AvaloniaMainActivity whose OnCreate runs AppBuilder.Configure<TApp>().UseAndroid().SetupWithoutStarting().Start() before InitializeAvaloniaView.
- Ensure Application.Current is set (call AppBuilder.Build().StartWithDesktopLifetime or SetupWithoutStarting) before InitializeAvaloniaView.
- Use AvaloniaMainActivity as the launcher, not a raw Activity.
- If AppBuilder.Build() threw, fix that earlier error — Application.Current stays null because of it.
Example fix
// before
private protected override void InitializeAvaloniaView(object? initialContent)
{
if (Avalonia.Application.Current is null)
throw new InvalidOperationException("Avalonia Application was not initialized. ...");
_view = new AvaloniaView(this) { Content = initialContent };
}
// after (build the app first in the AvaloniaMainActivity)
protected override void OnCreate(Bundle? savedInstanceState)
{
if (Avalonia.Application.Current is null)
AppBuilder.Configure<App>().UseAndroid().SetupWithoutStarting();
base.OnCreate(savedInstanceState);
} Defensive patterns
Strategy: validation
Validate before calling
// build the Avalonia app before any activity creates a view
if (Avalonia.Application.Current is null)
AppBuilder.Configure<App>().UseAndroid().SetupWithoutStarting(); Type guard
static bool AppInitialized => Avalonia.Application.Current is not null;
Try / catch
// initialization error — fix the bootstrap order; do not silently swallow.
Prevention
- Use AvaloniaMainActivity and build the app in its OnCreate.
- Call SetupWithoutStarting()/Start() before InitializeAvaloniaView.
- Inspect earlier AppBuilder exceptions.
When it happens
Trigger: InitializeAvaloniaView being called before AppBuilder.Build().Start() has created Application.Current; using a plain Android Activity (not an AvaloniaActivity/AvaloniaMainActivity) that never wires up the Avalonia app; accessing _view init during a config path that bypasses the app bootstrap.
Common situations: Subclassing AvaloniaActivity without an AvaloniaMainActivity that builds the app; calling OnCreate logic before Application is set; multi-activity apps where the launcher activity is not the Avalonia one; AppBuilder.Build() throwing silently earlier.
Related errors
- Unknown error: AvaloniaView initialization has failed.
- Application.Context.MainLooper was not expected to be null.
- This class should be instanciated from the UI thread
- Activity.Window must be set.
- The control isn't currently attached to a toplevel
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/da3e3ba8bd903f27.
Report an issue: GitHub.