I'm encountering the following exception in my Xamarin.Forms application on my GalaxyS22+ whenever the soft keyboard is removed from the view for any reason.

System.BadImageFormatException: Read out of bounds.
   at System.Reflection.Throw.OutOfBounds()
   at System.Reflection.Metadata.Ecma335.MethodDebugInformationTableReader.GetDocument(MethodDebugInformationHandle handle)
   at Mono.Debugging.Soft.PortablePdbData.GetDebugInfoFromPdb(MethodMirror method) in D:\a\_work\1\s\External\debugger-libs\Mono.Debugging.Soft\PortablePdbData.cs:line 186
   at Mono.Debugging.Soft.SoftDebuggerBacktrace.CreateStackFrame(StackFrame frame, Int32 frameIndex) in D:\a\_work\1\s\External\debugger-libs\Mono.Debugging.Soft\SoftDebuggerBacktrace.cs:line 157
   at Mono.Debugging.Soft.SoftDebuggerBacktrace.GetStackFrames(Int32 firstIndex, Int32 lastIndex) in D:\a\_work\1\s\External\debugger-libs\Mono.Debugging.Soft\SoftDebuggerBacktrace.cs:line 128
   at Mono.Debugging.Client.Backtrace.GetFrame(Int32 index, Int32 fetchMultipleCount) in D:\a\_work\1\s\External\debugger-libs\Mono.Debugging\Mono.Debugging.Client\Backtrace.cs:line 61
   at Mono.Debugging.Client.Backtrace..ctor(IBacktrace serverBacktrace) in D:\a\_work\1\s\External\debugger-libs\Mono.Debugging\Mono.Debugging.Client\Backtrace.cs:line 27
   at Mono.Debugging.Soft.SoftDebuggerSession.HandleBreakEventSet(Event[] es, Boolean dequeuing) in D:\a\_work\1\s\External\debugger-libs\Mono.Debugging.Soft\SoftDebuggerSession.cs:line 2186
   at Mono.Debugging.Soft.SoftDebuggerSession.EventHandler() in D:\a\_work\1\s\External\debugger-libs\Mono.Debugging.Soft\SoftDebuggerSession.cs:line 1802

The IDE I'm working out of is Visual Studio Community 2022, the Xamarin.Forms version is 5.0.0.2244, and my Android version is updated to the current release.

I've never encountered an issue like this and the stack trace doesn't make sense to me. I've tried swapping into Release mode, since it would seem that the issue is happening in the debugger, but the application still crashes at the same point. I checked into our crash reporting on the app center and it looks completely different from the stack trace I am getting from VS. Here is the beginning of that report:

Package: myApp
Version Code: 19
Version Name: 0.18
Android: 13
Android Build: TP1A.220624.014
Manufacturer: samsung
Model: SM-S906U
CrashReporter Key: 29ac0cda-bcdc-4ead-a7f5-bd7a6d812874
Start Date: 2023-07-13T14:11:12.247Z
Date: 2023-07-13T14:11:54.391Z

Xamarin Exception Stack:
Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0008e] in <3c553ea2ef344db484f0fd0644ad8207>:0
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00076] in <3c553ea2ef344db484f0fd0644ad8207>:0
  at Android.Views.ViewGroup.AddView (Android.Views.View child) [0x00031] in <8c63cbc465e14a70ade7127aa5abe07e>:0
  at Xamarin.Forms.Platform.Android.SwipeViewRenderer.UpdateContent () [0x0004c] in <b0894a26774f4d8c9615005666aba8aa>:0
  at Xamarin.Forms.Platform.Android.SwipeViewRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e) [0x00060] in <b0894a26774f4d8c9615005666aba8aa>:0
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000d3] in <b0894a26774f4d8c9615005666aba8aa>:0
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in <b0894a26774f4d8c9615005666aba8aa>:0
  at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in <b0894a26774f4d8c9615005666aba8aa>:0

The issue is not exclusive to my device, as I recreated the same problem on my colleague's Android tablet. Have not had a chance to run the application on iOS, so I'm not sure if this is Android OS exclusive or not, but I can report my findings there if there isn't enough information to go on here.

I've tried doing a clean and rebuild of the solution before deployment, deleting obj and bin folders for the project (which has cleared up several problems with VS in the past), trying different devices, and restarting VS. There were no changes to the exception.

I have tried using a different text field in my application for a test and the same issue is present. Dropdown type inputs work as they don't pull up the soft keyboard.

1

There are 1 best solutions below

0
Zach Murphy On

So I think we found the solution, but it wasn't an issue of the soft keyboard.

Xamarin's implementation MVVM infrastructure layers propagate all change events down through each view page in the current stack. On our home view, which serves as the basis for the entire application and is always in the stack, there was a ListView that did not have a specified CachingStrategy and, for some reason, couldn't utilize the default strategy.

When the soft keyboard was closing, it's likely that this was propagating changed events through each of the views in the stack, and since this ListView in question couldn't recycle one of its existing views, it tried to create a new one without first removing the parent, which, I'm assuming, is some sort of element that only allows a single, un-nested child.

That's my best guess anyway, since the problem resolved once we added CachingStrategy="RecycleElement" to the properties of the home page list.