{"record":{"id":"be40e39da4da6f71","repo":"dotnet/maui","slug":"loadview-must-be-called-before-accessing-view","errorCode":null,"errorMessage":"LoadView must be called before accessing View","messagePattern":"LoadView must be called before accessing View","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellSearchView.cs","lineNumber":37,"sourceCode":"using AView = Android.Views.View;\nusing LP = Android.Views.ViewGroup.LayoutParams;\n\nnamespace Microsoft.Maui.Controls.Platform.Compatibility\n{\n\tpublic class ShellSearchView : FrameLayout, IShellSearchView, TextView.IOnEditorActionListener, ITextWatcher\n\t{\n\t\t#region IShellSearchView\n\n\t\tpublic event EventHandler SearchConfirmed;\n\n\t\tpublic SearchHandler SearchHandler { get; set; }\n\n\t\tAView IShellSearchView.View\n\t\t{\n\t\t\tget\n\t\t\t{\n\t\t\t\tif (_searchButton == null)\n\t\t\t\t\tthrow new InvalidOperationException(\"LoadView must be called before accessing View\");\n\t\t\t\treturn this;\n\t\t\t}\n\t\t}\n\n\t\tvoid IShellSearchView.LoadView()\n\t\t{\n\t\t\tLoadView(SearchHandler);\n\t\t\tif (_searchHandlerAppearanceTracker == null)\n\t\t\t\t_searchHandlerAppearanceTracker = CreateSearchHandlerAppearanceTracker();\n\t\t}\n\n\t\tprotected virtual SearchHandlerAppearanceTracker CreateSearchHandlerAppearanceTracker()\n\t\t{\n\t\t\treturn new SearchHandlerAppearanceTracker(this, _shellContext);\n\t\t}\n\n\t\t#endregion IShellSearchView\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellSearchView.cs#L19-L55","documentation":"Thrown by the Android ShellSearchView when the IShellSearchView.View getter is accessed before IShellSearchView.LoadView() has been called. The getter checks if _searchButton is null (which is only initialized in LoadView) and throws InvalidOperationException. This enforces a strict initialization order: the search view's content must be inflated before the view itself is accessed.","triggerScenarios":"At line 37: `if (_searchButton == null) throw`. The _searchButton field is set inside LoadView(SearchHandler). Accessing the View property (via the IShellSearchView explicit interface implementation) before the Shell framework calls LoadView triggers this. This is typically an internal lifecycle ordering issue within the Shell search infrastructure, but could be triggered by custom SearchHandler renderers or premature access to the search view.","commonSituations":"1) Custom SearchHandler implementation or custom Shell search view that accesses View too early in the lifecycle. 2) MAUI framework version with a lifecycle ordering bug where the Shell calls View before LoadView. 3) Third-party library that hooks into Shell search rendering and accesses the view prematurely. 4) Rare race condition in fragment lifecycle where View is queried before LoadView completes.","solutions":["This is typically an internal framework lifecycle issue — update to the latest MAUI version where the ordering may be fixed.","If implementing a custom IShellSearchView, ensure LoadView() is called before any code path accesses View.","If subclassing ShellSearchView, call base.LoadView() first in any override before accessing View.","Avoid accessing the search view's View property from external code; let the Shell framework manage the lifecycle."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// When implementing IShellSearchView, ensure LoadView is called before View\nif (_searchButton == null)\n    LoadView(searchHandler);\n// Now safe to access View","typeGuard":null,"tryCatchPattern":null,"preventionTips":["If subclassing ShellSearchView, call base.LoadView() first in any override.","Let the Shell framework manage search view lifecycle — do not access View prematurely.","Update to the latest MAUI version for lifecycle fixes."],"tags":["maui","android","shell","search","renderer","lifecycle"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}