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

[Android] App crashes after adding a custom control #19953

Open Asfiroth opened 10 months ago

Asfiroth commented 10 months ago

Description

I've created a custom segmented control ´AnimatedSegmentedControl´, as I add it to a simple view it crashes the application, this only happens in Android on iOS it still works properly. This was working properly in previous maui versions.

Steps to Reproduce

  1. Create a Maui APP
  2. Create custom control
  3. Add custom control to page
  4. run on Android

Link to public reproduction project repository

https://github.com/Asfiroth/MauiCustomControlIssue

Version with bug

8.0.3

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

7.0.49

Affected platforms

Android

Affected platform versions

Android API 33

Did you find any workaround?

NO

Relevant log output

01-17 13:53:26.331 F/mono-rt (15611): [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-17 13:53:26.331 F/mono-rt (15611):    at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/obj/Release/net7.0/JniEnvironment.g.cs:line 20830
01-17 13:53:26.331 F/mono-rt (15611):    at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 75
01-17 13:53:26.331 F/mono-rt (15611):    at Android.Views.ViewGroup.AddView(View child) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Views.ViewGroup.cs:line 2148
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 41
01-17 13:53:26.331 F/mono-rt (15611):    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:line 56
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
01-17 13:53:26.331 F/mono-rt (15611):    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 2031
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\Layout\LayoutHandler.Android.cs:line 41
01-17 13:53:26.331 F/mono-rt (15611):    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:line 56
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
01-17 13:53:26.331 F/mono-rt (15611):    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 2031
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 42
01-17 13:53:26.331 F/mono-rt (15611):    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
01-17 13:53:26.331 F/mono-rt (15611):    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
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 47
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView) in D:\a\_work\1\s\src\Core\src\PropertyMapper.cs:line 82
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view) in D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:line 79
01-17 13:53:26.331 F/mono-rt (15611):    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
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view) in D:\a\_work\1\s\src\Core\src\Handlers\ContentView\ContentViewHandler.Android.cs:line 26
01-17 13:53:26.331 F/mono-rt (15611):    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
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 921
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value) in D:\a\_work\1\s\src\Controls\src\Core\Element\Element.cs:line 863
01-17 13:53:26.331 F/mono-rt (15611):    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 2031
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 96
01-17 13:53:26.331 F/mono-rt (15611):    at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context) in D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:line 127
01-17 13:53:26.331 F/mono-rt (15611):    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:line 32
01-17 13:53:26.331 F/mono-rt (15611):    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\net6.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:line 2031
01-17 13:53:26.331 F/mono-rt (15611):    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
01-17 13:53:26.331 F/mono-rt (15611):   --- End of managed Java.Lang.IllegalStateException stack trace ---
01-17 13:53:26.331 F/mono-rt (15611): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addViewInner(ViewGroup.java:5248)
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addView(ViewGroup.java:5077)
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addView(ViewGroup.java:5017)
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addView(ViewGroup.java:4989)
01-17 13:53:26.331 F/mono-rt (15611):   at crc6452ffdc5b34af3a0f.ScopedFragment.n_onCreateView(Native Method)
01-17 13:53:26.331 F/mono-rt (15611):   at crc6452ffdc5b34af3a0f.ScopedFragment.onCreateView(ScopedFragment.java:49)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3114)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:557)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3042)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2952)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:350)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:251)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1543)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.Activity.performStart(Activity.java:8330)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3670)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
01-17 13:53:26.331 F/mono-rt (15611):   at android.os.Handler.dispatchMessage(Handler.java:106)
01-17 13:53:26.331 F/mono-rt (15611):   at android.os.Looper.loopOnce(Looper.java:201)
01-17 13:53:26.331 F/mono-rt (15611):   at android.os.Looper.loop(Looper.java:288)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.ActivityThread.main(ActivityThread.java:7872)
01-17 13:53:26.331 F/mono-rt (15611):   at java.lang.reflect.Method.invoke(Native Method)
01-17 13:53:26.331 F/mono-rt (15611):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
01-17 13:53:26.331 F/mono-rt (15611):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
01-17 13:53:26.331 F/mono-rt (15611): 
01-17 13:53:26.331 F/mono-rt (15611):   --- End of managed Java.Lang.IllegalStateException stack trace ---
01-17 13:53:26.331 F/mono-rt (15611): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addViewInner(ViewGroup.java:5248)
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addView(ViewGroup.java:5077)
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addView(ViewGroup.java:5017)
01-17 13:53:26.331 F/mono-rt (15611):   at android.view.ViewGroup.addView(ViewGroup.java:4989)
01-17 13:53:26.331 F/mono-rt (15611):   at crc6452ffdc5b34af3a0f.ScopedFragment.n_onCreateView(Native Method)
01-17 13:53:26.331 F/mono-rt (15611):   at crc6452ffdc5b34af3a0f.ScopedFragment.onCreateView(ScopedFragment.java:49)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3114)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:557)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3042)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2952)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:350)
01-17 13:53:26.331 F/mono-rt (15611):   at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:251)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1543)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.Activity.performStart(Activity.java:8330)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3670)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
01-17 13:53:26.331 F/mono-rt (15611):   at android.os.Handler.dispatchMessage(Handler.java:106)
01-17 13:53:26.331 F/mono-rt (15611):   at android.os.Looper.loopOnce(Looper.java:201)
01-17 13:53:26.331 F/mono-rt (15611):   at android.os.Looper.loop(Looper.java:288)
01-17 13:53:26.331 F/mono-rt (15611):   at android.app.ActivityThread.main(ActivityThread.java:7872)
01-17 13:53:26.331 F/mono-rt (15611):   at java.lang.reflect.Method.invoke(Native Method)
01-17 13:53:26.331 F/mono-rt (15611):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
01-17 13:53:26.331 F/mono-rt (15611):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
ghost commented 10 months ago

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

XamlTest commented 9 months ago

Verified this on 8.0.6. Repro on Windows 11 and Android 14.0-API34, not repro on iOS 17.2 and MacCatalyst with below Project: CustomControl.zip