Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperationException: AutoCompleteViewHandler.Android -> DisconnectHandler #590

Closed
TalipV opened this issue Feb 24, 2024 · 3 comments
Closed
Labels
bug Something isn't working control-autocompleteview platform-android Something is related to Android
Milestone

Comments

@TalipV
Copy link

TalipV commented Feb 24, 2024

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 enisn added control-autocompleteview platform-android Something is related to Android bug Something isn't working labels Feb 25, 2024
@enisn enisn added this to the v2.8 milestone Feb 25, 2024
@enisn
Copy link
Owner

enisn commented Feb 25, 2024

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?

@TalipV
Copy link
Author

TalipV commented Feb 25, 2024

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
Copy link
Owner

enisn commented Feb 25, 2024

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

@enisn enisn closed this as completed Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working control-autocompleteview platform-android Something is related to Android
Projects
None yet
Development

No branches or pull requests

2 participants