enisn / UraniumUI

Uranium is a Free & Open-Source UI Kit for MAUI.
Apache License 2.0
1.17k stars 142 forks source link

InvalidOperationException: AutoCompleteViewHandler.Android -> DisconnectHandler #590

Closed TalipV closed 8 months ago

TalipV commented 8 months ago

In AutoCompleteViewHandler.Android the DisconnectHandler-override causes an exception when get_PlatFormView is called: InvalidOperationException - "PlatformView cannot be null here"

This occurs sometimes when I start my app and crashes the whole thing.

The reason seems to be that when the base, Microsoft.Maui.Handlers.ElementHandler, triggers DisconnectHandler it sets the PlatformView intentionally to null beforehand because "PlatformView access should be isolated to the instance passed into DisconnectHandler".

void IElementHandler.DisconnectHandler()
{
    if (PlatformView != null && VirtualView != null)
    {
        // We set the PlatformView to null so no one outside of this handler tries to access
        // PlatformView. PlatformView access should be isolated to the instance passed into
        // DisconnectHandler
        var oldPlatformView = PlatformView;
        PlatformView = null;
        DisconnectHandler(oldPlatformView);
    }
}

All it takes to fix this seems to be using the parameter "platformView" in AutoCompleteViewHandler.Android

protected override void DisconnectHandler(AppCompatAutoCompleteTextView platformView)
{
    PlatformView.TextChanged -= PlatformView_TextChanged;
    PlatformView.EditorAction -= PlatformView_EditorAction;
    PlatformView.ItemClick -= PlatformView_ItemClicked;
}

Maybe I am missing something as I am not that experienced with this stuff.

Here is the the exception:

android.runtime.JavaProxyThrowable: [System.InvalidOperationException]: PlatformView cannot be null here

 at Microsoft.Maui.Handlers.ViewHandler2[[UraniumUI.Controls.IAutoCompleteView, UraniumUI, Version=2.7.4.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatAutoCompleteTextView, Xamarin.AndroidX.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_PlatformView(Unknown Source:0)
 at UraniumUI.Handlers.AutoCompleteViewHandler.DisconnectHandler(Unknown Source:0)
 at Microsoft.Maui.Handlers.ViewHandler2[[UraniumUI.Controls.IAutoCompleteView, UraniumUI, Version=2.7.4.0, Culture=neutral, PublicKeyToken=null],[AndroidX.AppCompat.Widget.AppCompatAutoCompleteTextView, Xamarin.AndroidX.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnDisconnectHandler(Unknown Source:0)
 at Microsoft.Maui.Handlers.ViewHandler.OnDisconnectHandler(Unknown Source:0)
 at Microsoft.Maui.Handlers.ElementHandler.DisconnectHandler(Unknown Source:0)
 at Microsoft.Maui.Handlers.ElementHandler.Microsoft.Maui.IElementHandler.DisconnectHandler(Unknown Source:0)

 at Microsoft.Maui.Controls.Element.SetHandler(Unknown Source:0)
 at Microsoft.Maui.Controls.Element.set_Handler(Unknown Source:0)
 at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(Unknown Source:0)
 at Microsoft.Maui.Platform.ElementExtensions.ToHandler(Unknown Source:0)
 at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(Unknown Source:0)
 at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(Unknown Source:0)
 at Microsoft.Maui.Handlers.ViewHandler2[[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(Unknown Source:0)
 at Microsoft.Maui.Controls.Element.SetHandler(Unknown Source:0)
 at Microsoft.Maui.Controls.Element.set_Handler(Unknown Source:0)
 at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(Unknown Source:0)
 at Microsoft.Maui.Platform.ElementExtensions.ToHandler(Unknown Source:0)
 at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(Unknown Source:0)
 at Microsoft.Maui.Handlers.BorderHandler.UpdateContent(Unknown Source:0)
 at Microsoft.Maui.Handlers.BorderHandler.MapContent(Unknown Source:0)
 at Microsoft.Maui.PropertyMapper2+<>cDisplayClass5_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>b0(Unknown Source:0)
 at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(Unknown Source:0)
 at Microsoft.Maui.PropertyMapper.UpdateProperties(Unknown Source:0)
 at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(Unknown Source:0)
 at Microsoft.Maui.Handlers.ViewHandler2[[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(Unknown Source:0)
 at Microsoft.Maui.Handlers.BorderHandler.SetVirtualView(Unknown Source:0)
 at Microsoft.Maui.Handlers.ViewHandler2[[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(Unknown Source:0)
enisn commented 8 months ago

I made changes in https://github.com/enisn/UraniumUI/pull/591 that probably solves the problem but I couldn't make sure.

How can I reproduce the problem? Do you have reproduction steps?

TalipV commented 8 months ago

I made changes in #591 that probably solves the problem but I couldn't make sure.

How can I reproduce the problem? Do you have reproduction steps?

My app has an AutoCompleteTextField at its MainPage and my app is kept alive through a persistent notification. I can reproduce it when I close and reopen the app. 2.8.0-pre.5

It was working fine on my app when I pulled the branch"2.8-autocompleteview-handler-fix" and directly referenced it. So I guess your change was good. Thanks a lot!

enisn commented 8 months ago

Thanks for the feedback, it'll be released in the next release.