dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.12k stars 1.73k forks source link

[Android] "PlatformView cannot be null here" crash when switching shell tabs #22996

Open andreiionita opened 4 months ago

andreiionita commented 4 months ago

Description

After navigation away from a page, by changing tabs, that has a container like : public class SnackbarContainer : Layout<View> and going back to it, the android app will crash This is not an issue on iOS.

Steps to Reproduce

  1. Open the sample project
  2. Tap on "Go to second page" from the "Home" tab
  3. Tap on "Second Tab"
  4. Tap on "Home Tab"

Link to public reproduction project repository

https://github.com/andreiionita/Snackbar

Version with bug

8.0.21 SR4.1

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

I tested it with Android API 34

Did you find any workaround?

Wrap the page content with a grid, it will avoid the crash but it will cause other issues( for me it was a handler mapping action that was not called anymore)

Relevant log output

System.InvalidOperationException: PlatformView cannot be null here
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].get_PlatformView() in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 36
   at Microsoft.Maui.Controls.Compatibility.RendererToHandlerShim.PlatformArrange(Rect frame)
   at Microsoft.Maui.Controls.VisualElement.ArrangeOverride(Rect bounds) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1846
   at Microsoft.Maui.Controls.Compatibility.Layout.ArrangeOverride(Rect bounds) in D:\a_work\1\s\src\Controls\src\Core\Layout.cs:line 659
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.Arrange(Rect bounds) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1834
   at Microsoft.Maui.Controls.Compatibility.Layout.LayoutChildIntoBoundingRegion(VisualElement child, Rect region) in D:\a_work\1\s\src\Controls\src\Core\Layout.cs:line 220
   at Microsoft.Maui.Controls.Page.LayoutChildren(Double x, Double y, Double width, Double height) in D:\a_work\1\s\src\Controls\src\Core\Page\Page.cs:line 400
   at Microsoft.Maui.Controls.Page.UpdateChildrenLayout() in D:\a_work\1\s\src\Controls\src\Core\Page\Page.cs:line 525
   at Microsoft.Maui.Controls.Page.OnSizeAllocated(Double width, Double height) in D:\a_work\1\s\src\Controls\src\Core\Page\Page.cs:line 501
   at Microsoft.Maui.Controls.VisualElement.SizeAllocated(Double width, Double height) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1319
   at Microsoft.Maui.Controls.Page.OnChildMeasureInvalidated(VisualElement child, InvalidationTrigger trigger) in D:\a_work\1\s\src\Controls\src\Core\Page\Page.cs:line 565
   at Microsoft.Maui.Controls.Page.OnChildMeasureInvalidated(Object sender, EventArgs e) in D:\a_work\1\s\src\Controls\src\Core\Page\Page.cs:line 468
   at Microsoft.Maui.Controls.VisualElement.InvalidateMeasureInternal(InvalidationTrigger trigger) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1379
   at Microsoft.Maui.Controls.VisualElement.set_IsPlatformEnabled(Boolean value) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 1008
   at Microsoft.Maui.Controls.Compatibility.Platform.Android.Platform.<>c.<.cctor>b__66_0(BindableObject bindable, Object oldvalue, Object newvalue)
   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 643
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 569
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value) in D:\a_work\1\s\src\Controls\src\Core\BindableObject.cs:line 474
   at Microsoft.Maui.Controls.Compatibility.Platform.Android.Platform.SetRenderer(VisualElement bindable, IVisualElementRenderer value)
   at Microsoft.Maui.Controls.Compatibility.RendererToHandlerShim.SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2061
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
   at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 42
   at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 47
   at Microsoft.Maui.PropertyMapper`2.<>c_DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b_0(IElementHandler h, IElement v) in D:\a_work\1\s\src\Core\src\PropertyMapper.cs:line 172
   at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a_work\1\s\src\Core\src\PropertyMapper.cs:line 47
   at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a_work\1\s\src\Core\src\PropertyMapper.cs:line 82
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 53
   at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in D:\a_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 26
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view) in D:\a_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 56
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a_work\1\s\src\Controls\src\Core\VisualElement\VisualElement.cs:line 2061
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
   at Microsoft.Maui.Platform.ViewExtensions.ToHandler(IView view, IMauiContext context) in D:\a_work\1\s\src\Core\src\Platform\ViewExtensions.cs:line 44
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellContentFragment.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in D:\a_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellContentFragment.cs:line 145
   at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState) in C:\a_work\1\s\generated\androidx.fragment.fragment\obj\Release\net7.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:line 2031
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 367
github-actions[bot] commented 4 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

kevinxufei commented 4 months ago

Verified this issue with Visual Studio 17.11.0 Preview 2.0 (8.0.40 & 8.0.21 & 8.0.14). Can repro on Android platform with sample project. Snackbar-main.zip

davidortinau commented 4 months ago

@PureWeen testing this and it only happens when the page with this Compat Layout is the visible page within the Tab navigation stack. If you always navigate via a Shell.GotoAsync then it's fine.

@andreiionita have you tried a port of this snackbar layout to the .NET MAUI custom layout implementation? https://learn.microsoft.com/en-us/dotnet/maui/user-interface/layouts/custom?view=net-maui-8.0 Usually even if the Compat package works, you'll want to still upgrade to the .NET MAUI implementation.

ChristopherStephan commented 3 months ago

@davidortinau Thank you. Changing the snackbar layout to the .NET MAUI custom layout implementation worked for us.