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.28k stars 1.76k forks source link

[regression/7.0.0] PlatformView cannot be null #17165

Closed FreakyAli closed 9 months ago

FreakyAli commented 1 year ago

Description

I have a custom control that has a handler that Appends Mapping to it, When I do that and have a check for PlatformView, the application crashes at the null check when I close and reopen the app, Will add more information in the reproduction steps.

The Handler:

    public sealed partial class FreakyEntryHandler : EntryHandler
    {
        public FreakyEntryHandler()
        {
            Mapper.AppendToMapping("FreakyEntryCustomization", MapFreakyEntry);
        }

        private void MapFreakyEntry(IEntryHandler entryHandler, IEntry entry)
        {
            if (entry is FreakyEntry freakyEntry && entryHandler is FreakyEntryHandler freakyEntryHandler)
            {
                if (PlatformView != null && VirtualView != null)
                {
                    if (freakyEntry.ImageSource != default(ImageSource))
                    {
                        freakyEntryHandler.HandleAndAlignImageSourceAsync(freakyEntry).RunConcurrently();
                    }
                    HandleAllowCopyPaste(freakyEntry);
                }
            }
        }
    }

Steps to Reproduce

  1. Create a .Net Maui project with Android and iOS as supported platforms.
  2. Create a new Handler similar to the reproduction link.
  3. Set the page with your custom Entry as MainPage so that you can close the app and when you restart this page will be where you land.
  4. As soon as you restart this app, and land on your Entry's page you will get the below crash. (Note On iOS it's inconsistent, on Android this is 100% reproducible)
  5. Adding a video for understanding purposes(Could not add the VS part but I think the mobile part should be enough). Also, The repo branch is already configured to reproduce this issue

https://github.com/dotnet/maui/assets/31090457/3031749e-e66e-4315-a514-2d390c40c087

Link to public reproduction project repository

https://github.com/FreakyAli/Maui.FreakyControls/tree/entry_issue/MAUI.FreakyControls/Samples

Version with bug

7.0.92

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI, Yes, this used to work in Xamarin.Forms

Last version that worked well

6.0

Affected platforms

iOS, Android

Affected platform versions

Android API 33, iOS 16

Did you find any workaround?

No, which is a major problem because I am using this handler in one of my apps which has a login screen and I am sure other apps will have a similar problem too. The weirdest part is that my literal null check i.e. PlatformView != null is crashing which means it's probably crashing in the getter so there probably is no workaround that I can come up with for it.

Let me know if i can help with anything else.

Relevant log output

System.InvalidOperationException: PlatformView cannot be null here
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatEditText, Xamarin.AndroidX.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_PlatformView() in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:36
  at Maui.FreakyControls.FreakyEntryHandler.MapFreakyEntry(IEntryHandler entryHandler, IEntry entry) in /Users/gulamalihakim/Documents/Personal/Maui.FreakyControls/MAUI.FreakyControls/MAUI.FreakyControls/Shared/FreakyEntry/FreakyEntryHandler.cs:21
  at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass1_0`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<AppendToMapping>b__0(IEntryHandler handler, IEntry view, Action`2 action) in D:\a\_work\1\s\src\Core\src\PropertyMapperExtensions.cs:44
  at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass0_0`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ModifyMapping>g__newMethod|0(IEntryHandler handler, IEntry view) in D:\a\_work\1\s\src\Core\src\PropertyMapperExtensions.cs:23
  at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.Controls.Entry, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.EntryHandler, 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:172
  at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass1_0`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<AppendToMapping>b__0(IEntryHandler handler, IEntry view, Action`2 action) in D:\a\_work\1\s\src\Core\src\PropertyMapperExtensions.cs:43
  at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass0_0`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ModifyMapping>g__newMethod|0(IEntryHandler handler, IEntry view) in D:\a\_work\1\s\src\Core\src\PropertyMapperExtensions.cs:23
  at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.Controls.Entry, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.EntryHandler, 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:172
  at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass1_0`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<AppendToMapping>b__0(IEntryHandler handler, IEntry view, Action`2 action) in D:\a\_work\1\s\src\Core\src\PropertyMapperExtensions.cs:43
  at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass0_0`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IEntryHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<ModifyMapping>g__newMethod|0(IEntryHandler handler, IEntry view) in D:\a\_work\1\s\src\Core\src\PropertyMapperExtensions.cs:23
  at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.Controls.Entry, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.EntryHandler, 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:172
  at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:47
  at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:82
  at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:79
  at Microsoft.Maui.Handlers.ViewHandler.SetVirtualView(IElement element) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:134
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatEditText, Xamarin.AndroidX.AppCompat, 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:53
  at Microsoft.Maui.Handlers.EntryHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Entry\EntryHandler.Android.cs:37
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IEntry, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatEditText, Xamarin.AndroidX.AppCompat, 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:56
  at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:69
  at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:19
  at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:305
  at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96
  at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127
  at Microsoft.Maui.Handlers.BorderHandler.UpdateContent(IBorderHandler handler) in D:\a\_work\1\s\src\Core\src\Handlers\Border\BorderHandler.Android.cs:46
  at Microsoft.Maui.Handlers.BorderHandler.MapContent(IBorderHandler handler, IBorderView border) in D:\a\_work\1\s\src\Core\src\Handlers\Border\BorderHandler.Android.cs:63
  at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IBorderView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IBorderHandler, 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:172
  at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:47
  at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:82
  at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:79
  at Microsoft.Maui.Handlers.ViewHandler.SetVirtualView(IElement element) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:134
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IBorderView, 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:53
  at Microsoft.Maui.Handlers.BorderHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Border\BorderHandler.Android.cs:29
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IBorderView, 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:56
  at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:69
  at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:19
  at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:305
  at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96
  at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127
  at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:43
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, 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:56
  at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:69
  at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:19
  at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:305
  at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96
  at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127
  at Microsoft.Maui.Handlers.ScrollViewHandler.UpdateInsetView(IScrollView scrollView, IScrollViewHandler handler) in D:\a\_work\1\s\src\Core\src\Handlers\ScrollView\ScrollViewHandler.Android.cs:175
  at Microsoft.Maui.Handlers.ScrollViewHandler.MapContent(IScrollViewHandler handler, IScrollView scrollView) in D:\a\_work\1\s\src\Core\src\Handlers\ScrollView\ScrollViewHandler.Android.cs:109
  at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IScrollViewHandler, 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:172
  at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:47
  at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:82
  at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:79
  at Microsoft.Maui.Handlers.ViewHandler.SetVirtualView(IElement element) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:134
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.MauiScrollView, 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:53
  at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IScrollView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.MauiScrollView, 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:56
  at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:69
  at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:19
  at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:305
  at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96
  at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127
  at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:44
  at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:49
  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:172
  at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:47
  at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:82
  at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:79
  at Microsoft.Maui.Handlers.ViewHandler.SetVirtualView(IElement element) in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:134
  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:53
  at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:27
  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:56
  at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:69
  at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:19
  at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:305
  at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96
  at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:127
  at Microsoft.Maui.Platform.ScopedFragment.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) in D:\a\_work\1\s\src\Core\src\Platform\Android\Navigation\ScopedFragment.cs:22
  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 /Users/runner/work/1/s/generated/androidx.fragment.fragment/obj/Release/net6.0-android/generated/src/AndroidX.Fragment.App.Fragment.cs: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:352
drasticactions commented 1 year ago

https://github.com/dotnet/maui/blob/7d5aa8ca42896b4056c9315235250be5b7c151f4/src/Core/src/Handlers/View/ViewHandlerOfT.cs#L34-L44

It does throw if it's null, so I believe that's by design. The PlatformView and VirtualView shouldn't be null if you ask for them, so it may be that it's null at all is the bug, it shouldn't need to be something you check for. @PureWeen Is that right?

FreakyAli commented 1 year ago

@drasticactions The problem is even if i remove these null checks later on when I use them, the PlatformView would be null i.e. it will crash anyway!

PureWeen commented 1 year ago

don't use append from the ctor, that'll append to the static which will keep appending

Yea, I feel like something else in the code is removing the platformview.

If you follow our code against the stack trace the PlatformView should be set here https://github.com/dotnet/maui/blob/net7.0/src/Core/src/Handlers/Element/ElementHandler.cs#L50

And then UpdateProperties is called here which would can correlate to your stack trace

FreakyAli commented 1 year ago

@PureWeen In that case where do you suggest I append?

borrmann commented 1 year ago

@FreakyAli I think one option would be to call the mapping in the constructor of your class that inherits the view, not the handler. At least there is an example given for a ContentPage docs that appends handlers in its constructor.

        public FreakyEntry()
        {
            AppendMappings();
        }

        public partial void AppendMappings();

Then you could implement the method for instance like so on iOS:

 public partial void AppendMappings()
 {
     Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("FreakyEntry", (handler, view) =>
     {
         if (view is FreakyEntry freakyEntry && handler is FreakyEntryHandler freakyEntryHandler)
         {
             if (freakyEntry .PlatformView is not null && freakyEntryHandler.VirtualView is not null)
             {                 
                         freakyEntryHandler.PlatformView.TextContentType = UITextContentType.TelephoneNumber;
                         freakyEntryHandler.PlatformView.KeyboardType = UIKeyboardType.PhonePad;
             }
         }
     });
 }

and then append the handler in MauiProgram

.ConfigureMauiHandlers(handlers =>
{
    handlers.AddHandler<FreakyEntry , Handler.FreakyEntryHandler>();
}

and FreakyEntryHandler only needs to inherit from EntryHandler, but could also do other stuff that you would do OnConnect().

flemingho commented 1 year ago

FWIW, I came across this error in my Android app.

In the app my MainPage navigated to another page like so

private async void ToDo_OnClicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new ToDo());
}

With the ToDo page open I would close the app without leaving the page (and not just navigate away to another app). Upon opening/launching the app again I would get this PlatformView cannot be null error caught in the debugger on the base.OnStart() event here

public class MainActivity : MauiAppCompatActivity
{
    protected override void OnStart()
    {
        base.OnStart();
...
}

The way I think I solved the issue was to remove the ToDo page from the navigation stack

public partial class ToDo : ContentPage
{
...

    protected override async void OnDisappearing()
    {
        await Navigation.PopAsync();    // fix for "Platform View Cannot be null here" error.???
        base.OnDisappearing();
    }
 ...
}

I'm not sure if I handled this correctly, but it has allowed me to move on with development.

Hopefully, this will shed some light.

FreakyAli commented 12 months ago

@borrmann I tried that and it still crashes, I can get around it by adding a try-catch, but I think the MAUI team should fix this...

kevinxufei commented 9 months ago

Verified this issue using Visual Studio 17.10.0 Preview 1 (maui version:8.0.6). The issue is not reproduced on Android (API34/33)/iOS (17.0/17.2) platforms using the sample project. maui1

ghost commented 9 months ago

Hi @FreakyAli. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

FreakyAli commented 9 months ago

This seems to be fixed with version 8.0.7, Thanks!