dotnet/maui · critical · InvalidOperationException
Call Forms.Init (Activity, Bundle) before this
Error message
Call Forms.Init (Activity, Bundle) before this
What it means
Thrown by FormsAppCompatActivity.InternalSetPage when the static Forms.IsInitialized flag is false. Setting the main page requires the Forms platform (registrar, services, device info) to be initialized via Forms.Init(Activity, Bundle). The guard prevents the page from being created against an uninitialized platform, which would crash later with obscure NullReferenceExceptions.
Source
Thrown at src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs:435
SettingMainPage();
}
}
void CheckForAppLink(Intent intent)
{
string action = intent.Action;
string strLink = intent.DataString;
if (Intent.ActionView != action || string.IsNullOrWhiteSpace(strLink))
return;
var link = new Uri(strLink);
_application?.SendOnAppLinkRequestReceived(link);
}
void InternalSetPage(Page page)
{
if (!Forms.IsInitialized)
throw new InvalidOperationException("Call Forms.Init (Activity, Bundle) before this");
if (Platform != null)
{
Platform.SetPage(page);
return;
}
PopupManager.ResetBusyCount(this);
Platform = new Platform(this);
Platform.SetPage(page);
_layout.AddView(Platform);
_layout.BringToFront();
}
void OnStateChanged()
{
if (_application == null)View on GitHub (pinned to f377ff1c5e)
Solutions
- Call Forms.Init(this, bundle) at the top of OnCreate, before LoadApplication or any SetMainPage call.
- Ensure Forms.Init is not gated behind a condition that can be false at runtime (e.g., a design-mode check leaking into production).
- If you replaced the default Forms initialization with a custom setup, verify it still sets Forms.IsInitialized (via Forms.Init or Forms.SetFlags followed by Init).
Example fix
// before
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetMainPage(new MainPage()); // throws
}
// after
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
LoadApplication(new App());
} Defensive patterns
Strategy: validation
Validate before calling
if (!Forms.IsInitialized)
throw new InvalidOperationException("Call Forms.Init(this, bundle) before setting the main page.");
InternalSetPage(page); Prevention
- Centralize initialization: call Forms.Init then LoadApplication consecutively in OnCreate.
- Never call SetMainPage from callbacks that can fire before OnCreate completes.
- Add a debug assertion that Forms.IsInitialized is true before any SetMainPage call.
When it happens
Trigger: InternalSetPage (and thus SetMainPage) is called before Forms.Init(this, bundle) runs in OnCreate. This can happen when an app link callback, a deep-link handler, or custom lifecycle code calls SetMainPage, or when Forms.Init is conditionally skipped (e.g., behind a design-time check).
Common situations: Forgetting Forms.Init in the Activity's OnCreate. Reordering initialization so LoadApplication or SetMainPage precedes Forms.Init. Design-time/previewer paths that skip Forms.Init but still call SetMainPage.
Related errors
- Activity OnCreate was not called prior to loading the applic
- call Forms.Init() before this
- call Forms.Init() before this
- LoadView must be called before accessing View
- MauiContext not set
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/82cf0d6cf63f5bc0.
Report an issue: GitHub.