{"record":{"id":"a22d0708a8bf4706","repo":"dotnet/maui","slug":"toolbarresource-must-be-set-to-a-androidx-appcompa","errorCode":null,"errorMessage":"ToolbarResource must be set to a androidx.appcompat.widget.Toolbar","messagePattern":"ToolbarResource must be set to a androidx\\.appcompat\\.widget\\.Toolbar","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs","lineNumber":233,"sourceCode":"\t\t\tif (_toolbarResource == 0)\n\t\t\t{\n\t\t\t\tToolbarResource = Resource.Layout.toolbar;\n\t\t\t}\n\n\t\t\tif (_tabLayoutResource == 0)\n\t\t\t{\n\t\t\t\t_tabLayoutResource = Resource.Layout.tabbar;\n\t\t\t}\n\n\t\t\tif (ToolbarResource != 0)\n\t\t\t{\n\t\t\t\ttry\n\t\t\t\t{\n\t\t\t\t\tbar = LayoutInflater.Inflate(ToolbarResource, null).JavaCast<AToolbar>();\n\t\t\t\t}\n\t\t\t\tcatch (global::Android.Views.InflateException ie)\n\t\t\t\t{\n\t\t\t\t\tthrow new InvalidOperationException(\"ToolbarResource must be set to a androidx.appcompat.widget.Toolbar\", ie);\n\t\t\t\t}\n\n\t\t\t\tif (bar == null)\n\t\t\t\t\tthrow new InvalidOperationException(\"ToolbarResource must be set to a androidx.appcompat.widget.Toolbar\");\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tbar = new AToolbar(this);\n\t\t\t}\n\n\t\t\tSetSupportActionBar(bar);\n\n\t\t\tProfile.FramePartition(\"SetContentView\");\n\t\t\t_layout = new ARelativeLayout(BaseContext);\n\t\t\tSetContentView(_layout);\n\n\t\t\tProfile.FramePartition(\"OnStateChanged\");\n\t\t\tMicrosoft.Maui.Controls.Application.Current = null;","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/AppCompat/FormsAppCompatActivity.cs#L215-L251","documentation":"Thrown during OnCreate when inflating the layout referenced by ToolbarResource raises an Android.Views.InflateException. The framework wraps that exception to clarify that the inflated layout must be an androidx.appcompat.widget.Toolbar. The InflateException itself usually means the layout resource is missing, references an unknown view, or a dependency (e.g., AppCompat/Material library) is absent.","triggerScenarios":"ToolbarResource is set to a layout ID whose root tag is not a toolbar widget, or to a resource that does not exist. A missing or version-mismatched AndroidX AppCompat/Material NuGet prevents the toolbar widget from inflating. Proguard/R8 stripping the toolbar class can also cause inflation failure.","commonSituations":"Upgrading AndroidX AppCompat or Material packages to incompatible versions. Setting ToolbarResource to a custom layout that was removed or renamed. ProGuard/R8 configuration stripping AToolbar in release builds. Setting ToolbarResource = Resource.Layout.NonExistentLayout.","solutions":["Ensure ToolbarResource points to a layout whose root element is androidx.appcompat.widget.Toolbar (the default Resource.Layout.toolbar).","Verify the AndroidX AppCompat and Material NuGet packages are referenced and version-aligned across the solution.","If using ProGuard/R8, add keep rules for androidx.appcompat.widget.Toolbar and its inflator.","Inspect the inner InflateException (passed as the InnerException) for the exact inflation error and resolve the referenced view or resource."],"exampleFix":"// before\n[Activity(ToolbarResource = Resource.Layout.my_broken_layout)]\n\n// after\n[Activity] // lets Forms default ToolbarResource = Resource.Layout.toolbar\n// or explicitly\n[Activity(ToolbarResource = Resource.Layout.toolbar)]","handlingStrategy":"try-catch","validationCode":"// Validate the layout resource exists and its root is a toolbar before OnCreate runs.\nbool IsToolbarLayout(int resourceId)\n{\n    try\n    {\n        var view = LayoutInflater.Inflate(resourceId, null);\n        return view is AndroidX.AppCompat.Widget.Toolbar;\n    }\n    catch\n    {\n        return false;\n    }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    // OnCreate runs normally\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ToolbarResource\"))\n{\n    // Fallback: unset ToolbarResource so Forms creates a default AToolbar\n    ToolbarResource = 0;\n    Log.Warn(nameof(FormsAppCompatActivity), $\"Toolbar layout invalid, falling back to default toolbar. Inner: {ex.InnerException?.Message}\");\n    throw; // or re-run OnCreate logic with default toolbar\n}","preventionTips":["Pin AndroidX AppCompat and Material NuGet versions consistently across all projects in the solution.","Add ProGuard/R8 keep rules for androidx.appcompat.widget.Toolbar.","Validate toolbar layout resources in a release-config integration test.","Inspect the InnerException (InflateException) for the root cause before changing the layout."],"tags":["android","toolbar","layout-inflation","androidx","appcompat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}