I have a legacy Xamarin forms app to which I am trying to add a tabbed page as the root page. This is how my tabbed page xaml looks like-
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ChildPage1="clr-namespace:sampleApp;assembly=sampleAssembly">
<NavigationPage Title="Items" IconImageSource="icon_items_tab.png">
<x:Arguments>
<ChildPage1:ItemListPage BindingContext="{Binding ItemListTabVM}" />
</x:Arguments>
</NavigationPage>
<ContentPage Title="Messages" IconImageSource="icon_messages_tab.png" />
<ContentPage Title="Search" IconImageSource="icon_search_tab.png" />
</TabbedPage>
When I run the app I am getting the below unhandled exception-
Android.Views.InflateException: Binary XML file line #1 in com.sample.xamarinapp:layout/toolbar: Binary XML file line #1 in com.sample.xamarinapp:layout/toolbar: Error inflating class <unknown>
Below is the internal exception details that I pulled from the thread-
System.Diagnostics.Debugger.Mono_UnhandledException_internal()
in System.Diagnostics.Debugger.Mono_UnhandledException_internal
in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Diagnostics/Debugger.cs:125,4
in Android.Runtime.DynamicMethodNameCounter.104
in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw at /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:157,13
in Java.Interop.JniEnvironment.InstanceMethods.CallObjectMethod
in Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualObjectMethod
in Android.Views.LayoutInflater.Inflate at /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-29/mcw/Android.Views.LayoutInflater.cs:661,5
in Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.SetupToolbar at D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:789,5
in Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnElementChanged at D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:347,6
in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.NavigationPage>.SetElement at D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:241,4
in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.NavigationPage>.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement at D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131,4
in Xamarin.Forms.Platform.Android.Platform.CreateRenderer at D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:404,4
in Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnCreateView at D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:57,5
in AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ at D:\a\1\s\generated\androidx.fragment.fragment\obj\Release\monoandroid9.0\generated\src\AndroidX.Fragment.App.Fragment.cs:1977,4
in Android.Runtime.DynamicMethodNameCounter.104
in Java.Interop.NativeMethods.java_interop_jnienv_call_nonvirtual_void_method_a
in Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod
in Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeNonvirtualVoidMethod
in Android.Views.View.Measure at /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-29/mcw/Android.Views.View.cs:16637,5
in Xamarin.Forms.Platform.Android.AppCompat.TabbedPageRenderer.OnLayout at D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\TabbedPageRenderer.cs:398,5
in Xamarin.Forms.Platform.Android.FormsViewGroup.n_OnLayout_ZIIII at D:\a\1\s\Xamarin.Forms.Platform.Android.FormsViewGroup\obj\Release\generated\src\Xamarin.Forms.Platform.Android.FormsViewGroup.cs:206,4
in Android.Runtime.DynamicMethodNameCounter.40
in Java.Interop.NativeMethods.java_interop_jnienv_call_nonvirtual_void_method_a
in Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod
in Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod
in Xamarin.Forms.Platform.Android.FormsViewGroup.MeasureAndLayout at D:\a\1\s\Xamarin.Forms.Platform.Android.FormsViewGroup\obj\Release\generated\src\Xamarin.Forms.Platform.Android.FormsViewGroup.cs:189,5
in Xamarin.Forms.Platform.Android.VisualElementTracker.UpdateLayout at D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementTracker.cs:111,5
in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.TabbedPage>.UpdateLayout at D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:139,4
in Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout at D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:235,4
in Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout at D:\a\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:78,4
in Android.Views.ViewGroup.n_OnLayout_ZIIII at /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-29/mcw/Android.Views.ViewGroup.cs:3612,4
in Android.Runtime.DynamicMethodNameCounter.34
If I remove the NavigationPage wrapper and just place the ItemList child page as content page, application works with out any issues. Why is it throwing an exception when child page is wrapped inside a navigation page? Thanks in advance for any help. I am using VS 2019, XF 5.0.0.2244
CodePudding user response:
At first, you need to add the reference of the page in the TabbedPage, such as:
xmlns:local="clr-namespace:TabbedPageWithNavigationPage;assembly=TabbedPageWithNavigationPage"
And then use the reference of it in the NavigationPage wrapper, such as:
<NavigationPage Title="Schedule" IconImageSource="schedule.png">
<x:Arguments>
<local:SchedulePage />
</x:Arguments>
</NavigationPage>
If you need more information, please check the official document to how to use the NavigationPage wrapper: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/tabbed-page
CodePudding user response:
Alright, after spending some time, I finally resolved the issue. Here is what I did-
In the internal error information that I shared in my question, there was a mention about "Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout". So, I created a custom renderer using TabbedPageRenderer and created an override to 'OnLayout()'. When I ran the code, I got the same exception at base.OnLayout(changed, left, top, right, bottom), but this time with more information and stack trace.
The stack trace showed the Actual error -
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 37: TypedValue{t=0x2/d=0x7f030007 a=6} at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:781) at android.view.View.<init>(View.java:5522)
So, I did a search on this particular exception and this post gave me some idea.
I added a Toolbar.xml under Resource/Layout. I removed the below themes that we usually find in the Toolbar.xml.
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
After that, my application started working. Tabs display the navigation pages with out any issues.
By the way I used the Toolbar.xml content from here.