{"record":{"id":"82cf0d6cf63f5bc0","repo":"dotnet/maui","slug":"call-forms-init-activity-bundle-before-this","errorCode":null,"errorMessage":"Call Forms.Init (Activity, Bundle) before this","messagePattern":"Call Forms\\.Init \\(Activity, Bundle\\) before this","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs","lineNumber":435,"sourceCode":"\t\t\t\tSettingMainPage();\n\t\t\t}\n\t\t}\n\n\t\tvoid CheckForAppLink(Intent intent)\n\t\t{\n\t\t\tstring action = intent.Action;\n\t\t\tstring strLink = intent.DataString;\n\t\t\tif (Intent.ActionView != action || string.IsNullOrWhiteSpace(strLink))\n\t\t\t\treturn;\n\n\t\t\tvar link = new Uri(strLink);\n\t\t\t_application?.SendOnAppLinkRequestReceived(link);\n\t\t}\n\n\t\tvoid InternalSetPage(Page page)\n\t\t{\n\t\t\tif (!Forms.IsInitialized)\n\t\t\t\tthrow new InvalidOperationException(\"Call Forms.Init (Activity, Bundle) before this\");\n\n\t\t\tif (Platform != null)\n\t\t\t{\n\t\t\t\tPlatform.SetPage(page);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tPopupManager.ResetBusyCount(this);\n\n\t\t\tPlatform = new Platform(this);\n\t\t\tPlatform.SetPage(page);\n\t\t\t_layout.AddView(Platform);\n\t\t\t_layout.BringToFront();\n\t\t}\n\n\t\tvoid OnStateChanged()\n\t\t{\n\t\t\tif (_application == null)","sourceCodeStart":417,"sourceCodeEnd":453,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs#L417-L453","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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)."],"exampleFix":"// before\nprotected override void OnCreate(Bundle bundle)\n{\n    base.OnCreate(bundle);\n    SetMainPage(new MainPage()); // throws\n}\n\n// after\nprotected override void OnCreate(Bundle bundle)\n{\n    base.OnCreate(bundle);\n    Forms.Init(this, bundle);\n    LoadApplication(new App());\n}","handlingStrategy":"validation","validationCode":"if (!Forms.IsInitialized)\n    throw new InvalidOperationException(\"Call Forms.Init(this, bundle) before setting the main page.\");\nInternalSetPage(page);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["android","forms-init","initialization","lifecycle"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}