Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] Java.Lang.IndexOutOfBoundsException: setSpan #12762

Closed
Transis-Felipe opened this issue Nov 6, 2020 · 1 comment · Fixed by #12764
Closed

[Bug] Java.Lang.IndexOutOfBoundsException: setSpan #12762

Transis-Felipe opened this issue Nov 6, 2020 · 1 comment · Fixed by #12764
Labels
a/entry e/2 🕑 2 in-progress This issue has an associated pull request that may resolve it! p/Android t/bug 🐛

Comments

@Transis-Felipe
Copy link
Contributor

Transis-Felipe commented Nov 6, 2020

Description

IndexOutOfBoundsException observed in Xamarin.Android/Xamarin.Forms code in the wild.

Stack trace:

JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args)
JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters)
EditText.SetSelection (System.Int32 index)
EntryRendererBase`1[TControl].UpdateText ()
EntryRendererBase`1[TControl].OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e)
(wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs)
BindableObject.OnPropertyChanged (System.String propertyName)
Element.OnPropertyChanged (System.String propertyName)
BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent)
BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes)
BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes)
Element.SetValueFromRenderer (Xamarin.Forms.BindableProperty property, System.Object value)
TextTransformUtilites.SetPlainText (Xamarin.Forms.InputView inputView, System.String nativeText)
ITextWatcher.OnTextChanged (Java.Lang.ICharSequence s, System.Int32 start, System.Int32 before, System.Int32 count)
ITextWatcherInvoker.n_OnTextChanged_Ljava_lang_CharSequence_III (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_s, System.Int32 start, System.Int32 before, System.Int32 count)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.74(intptr,intptr,intptr,int,int,int)
java.lang.IndexOutOfBoundsException: setSpan (16 ... 16) ends beyond length 15
android.text.SpannableStringBuilder.checkRange SpannableStringBuilder.java:1331
android.text.SpannableStringBuilder.setSpan SpannableStringBuilder.java:690
android.text.SpannableStringBuilder.setSpan SpannableStringBuilder.java:682
android.text.Selection.setSelection Selection.java:94
android.text.Selection.setSelection Selection.java:78
android.text.Selection.setSelection Selection.java:153
android.widget.EditText.setSelection EditText.java:136
crc643f46942d9dd1fff9.EntryRendererBase_1.n_onTextChanged(Native Method)
crc643f46942d9dd1fff9.EntryRendererBase_1.onTextChanged(Unknown Source:0)
android.widget.TextView.sendOnTextChanged TextView.java:10587
android.widget.TextView.handleTextChanged TextView.java:10677
android.widget.TextView$ChangeWatcher.onTextChanged TextView.java:13464
android.text.SpannableStringBuilder.sendTextChanged SpannableStringBuilder.java:1273
android.text.SpannableStringBuilder.replace SpannableStringBuilder.java:576
android.text.SpannableStringBuilder.replace SpannableStringBuilder.java:507
android.text.SpannableStringBuilder.replace SpannableStringBuilder.java:37
android.text.method.NumberKeyListener.onKeyDown NumberKeyListener.java:131
android.widget.TextView.doKeyDown TextView.java:8470
android.widget.TextView.onKeyDown TextView.java:8227
android.view.KeyEvent.dispatch KeyEvent.java:2801
android.view.View.dispatchKeyEvent View.java:13403
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
androidx.core.widget.NestedScrollView.dispatchKeyEvent(Unknown Source:0)
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
android.view.ViewGroup.dispatchKeyEvent ViewGroup.java:1922
com.android.internal.policy.DecorView.superDispatchKeyEvent DecorView.java:473
com.android.internal.policy.PhoneWindow.superDispatchKeyEvent PhoneWindow.java:1869
android.app.Activity.dispatchKeyEvent Activity.java:3964
androidx.core.app.ComponentActivity.superDispatchKeyEvent(Unknown Source:0)
androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(Unknown Source:10)
androidx.core.app.ComponentActivity.dispatchKeyEvent(Unknown Source:18)
androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(Unknown Source:22)
androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(Unknown Source:2)
androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(Unknown Source:8)
androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(Unknown Source:2)
com.android.internal.policy.DecorView.dispatchKeyEvent DecorView.java:387
android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent ViewRootImpl.java:5460
android.view.ViewRootImpl$ViewPostImeInputStage.onProcess ViewRootImpl.java:5328
android.view.ViewRootImpl$InputStage.deliver ViewRootImpl.java:4835
android.view.ViewRootImpl$InputStage.onDeliverToNext ViewRootImpl.java:4888
android.view.ViewRootImpl$InputStage.forward ViewRootImpl.java:4854
android.view.ViewRootImpl$AsyncInputStage.forward ViewRootImpl.java:4994
android.view.ViewRootImpl$InputStage.apply ViewRootImpl.java:4862
android.view.ViewRootImpl$AsyncInputStage.apply ViewRootImpl.java:5051
android.view.ViewRootImpl$InputStage.deliver ViewRootImpl.java:4835
android.view.ViewRootImpl$InputStage.onDeliverToNext ViewRootImpl.java:4888
android.view.ViewRootImpl$InputStage.forward ViewRootImpl.java:4854
android.view.ViewRootImpl$InputStage.apply ViewRootImpl.java:4862
android.view.ViewRootImpl$InputStage.deliver ViewRootImpl.java:4835
android.view.ViewRootImpl.deliverInputEvent ViewRootImpl.java:7605
android.view.ViewRootImpl.doProcessInputEvents ViewRootImpl.java:7539
android.view.ViewRootImpl.enqueueInputEvent ViewRootImpl.java:7500
android.view.ViewRootImpl$ViewRootHandler.handleMessage ViewRootImpl.java:4608
android.os.Handler.dispatchMessage Handler.java:107
android.os.Looper.loop Looper.java:241
android.app.ActivityThread.main ActivityThread.java:7617
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:492
com.android.internal.os.ZygoteInit.main ZygoteInit.java:941

Steps to Reproduce

Unknown

Expected Behavior

No crash

Actual Behavior

Java.Lang.IndexOutOfBoundsException: setSpan (16 ... 16) ends beyond length 15

Basic Information

  • Version with issue: 4.8.0.1560
  • Last known good version: n/a
  • IDE: VS 2019 16.7
  • Platform Target Frameworks:
    • iOS: -
    • Android: API Level 29 Q
    • UWP: -
  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices: motorola one (Android 10), moto g(7) power (Android 10)

Reproduction Link

This has been observed via AppCenter and is demonstrably rare. Thus, I have no reproduction.

@Transis-Felipe Transis-Felipe added s/unverified New report that has yet to be verified t/bug 🐛 labels Nov 6, 2020
@Transis-Felipe
Copy link
Contributor Author

Transis-Felipe commented Nov 6, 2020

I propose this update on Xamarin.Forms.Platform.Android\Renderers\EntryRenderer.cs:

if (text.Length < EditText.Length)
{
   EditText.SetSelection(text.Length);
}

or a Try/Catch like void UpdateCursorSelection()


				try
				{
					int start = GetSelectionStart();
					int end = GetSelectionEnd(start);

					EditText.SetSelection(start, end);
				}
				catch (System.Exception ex)
				{
					Internals.Log.Warning("Entry", $"Failed to set Control.Selection from CursorPosition/SelectionLength: {ex}");
				}

@rmarinho rmarinho added p/Android a/entry e/2 🕑 2 and removed s/unverified New report that has yet to be verified labels Nov 6, 2020
@jsuarezruiz jsuarezruiz added the in-progress This issue has an associated pull request that may resolve it! label Dec 16, 2020
rmarinho added a commit that referenced this issue Jul 8, 2021
…12764) Fix to Issue #12762

* Update EntryRenderer.cs

Fix to Issue #12762
Java.Lang.IndexOutOfBoundsException: setSpan (nn ... nn) ends beyond length n-1

* Fix to EntryRenderer.cs

* Update EntryRenderer.cs

* Update EntryRenderer.cs

Co-authored-by: Rui Marinho <me@ruimarinho.net>
rmarinho added a commit that referenced this issue Sep 20, 2021
* Fix 4143: improved Span region calculation (#13348) fixes #4143 fixes #6992 fixes #11650 fixes #7655 fixes #11657 fixes #10520 fixes #4829 

* improved span region calculation on android

* Remove unused code

* Nit: clean up comments

* Wrap in ScrollView for small screen compatibility

Co-authored-by: Tim Dittmar <Tim.Dittmar@Exxeta.com>
Co-authored-by: Rachel Kang <rachel.j.kang@gmail.com>
Co-authored-by: Rachel Kang <rachelkang@microsoft.com>

* Only check for netfx when not in a wapproj (#14402) fixes #13957

* [macOS] Update Switch renderer (#14334) fixes #14313

* [macOS] Update Switch renderer #14313

* [macOS] Update Switch renderer #14313

* [Android] Fix to Issue Java.Lang.IndexOutOfBoundsException: setSpan (#12764) Fix to Issue #12762

* Update EntryRenderer.cs

Fix to Issue #12762
Java.Lang.IndexOutOfBoundsException: setSpan (nn ... nn) ends beyond length n-1

* Fix to EntryRenderer.cs

* Update EntryRenderer.cs

* Update EntryRenderer.cs

Co-authored-by: Rui Marinho <me@ruimarinho.net>

* [Android] Fix ClearButton not working when changing the ClearButtonVisibility while the Entry field is focused (#13820) Fixes #13819

Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>

* Automated dotnet-format update (#14404)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Automated dotnet-format update (#14405)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix memory leak (and some crashes) in ItemsViewController (iOS) (#14111)

* add unit test

* fix memory leak (and some crashes) in ItemsViewController (iOS)

* Update ItemsViewController.cs

Co-authored-by: Rui Marinho <me@ruimarinho.net>

* Enable ScrollTo tests for Android, UWP; implement ScrollTo with group index for Android (#13007)

* Remove inaccurate category

* Enable ScrollTo tests for Android/UWP; Implement grouped ScrollTo for Android

* Implement missing ScrollTo grouped item by index

* Exempt 3788 test from UWP

* Ignore test on UWP

Co-authored-by: Rui Marinho <me@ruimarinho.net>

* Fix type for case blueviolet (#14434)

The 'v' in this case was uppercase causing this typeconverter to reject BlueViolet as a valid color value. Fixes #AB1286706 [XVS]XLS0431: Invalid value for property 'BackgroundColor': 'BlueViolet'

* [iOS] Fix: CollectionView was not updating when it was invisible (#14384)

* [iOS] Fix: CollectionView was not updating when it was invisible

* [iOS] Fix: NSInternalInconsistencyException Reason: request for number of items in section X when there are only X sections in the collection view when NumberOfItemsInSection is invoked

* Revert "[iOS] Fix: NSInternalInconsistencyException Reason: request for number of items in section X when there are only X sections in the collection view when NumberOfItemsInSection is invoked"

This reverts commit 2343f15.

* [iOS] Fix: Prevent NumberOfItemsInSection invoking if CollectionView is hidded to avoid  "NSInternalInconsistencyException Reason: request for number of items in section X when there are only X sections"

* [iOS] Scroll locked issue using SwipeView (#12758)

* Fixed issue re-enabling the scroll

* Added repro sample

* Fixed also the swipe sensitive issue on iOS

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Fix removed shadow in Thumb setting a custom color (#13166)

* [iOS] Fix crash/closing wrong modal with FormSheet and tap outside (#14527)

* Add repro + fix

* Update Issue12300.cs

* [Core] SwipeItem Parent using SwipeView in DataTemplate (#13385)

* Added repro sample

* Fix the issue

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Fix Frame Background issue with transparent color (#14565)

* Fix broken disabled Button visual state in UWP (#14567)

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Update Device.Idiom when flipping tablet mode state (#13150)

* [Android] Fix occasional wrong touch interception in SwipeView Content  (#13732)

* Fix wrong touch interception in SwipeView Content on Android

* Updated swipe delta

* Fix Entry issue using TextColor and ClearButton in iOS < 13 (#14566)

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Fix issue using FormattedString and LineBreakMode on iOS (#14572)

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Align BarBackground behavior between Android and iOS. (#13503)

* Automated dotnet-format update (#14570)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* [Android] Update gradients based on offset changes in Frame and BoxView (#11812)

* Update gradients based on offset changes in Frame and BoxView on Android

* Updated the sample to test also the BoxView Brush offsets

* Fix NRE in ListView Command validation (#14580)

* [iOS] Fix inability to check an initially disabled RadioButton after enabling it (#14545)

* Listen for updates to IsEnabled on RadioButton to ensure that the tap gesture recogniser is added when the control is enabled

* Update Issue14544.cs

* Update test case

Co-authored-by: Gerald Versluis <gerald@verslu.is>

* Fix crash navigating in Shell (#14577)

* Androidx bumps (#14506)

* Update to Latest Android X Libraries

* - fix android forwarders

* Moar updates

Co-authored-by: Gerald Versluis <gerald@verslu.is>

* Validate issue 14433 (#14576)

* Added repro sample

* Updated sample

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* [iOS] Remove usage of System.Drawing types (#14571)

* Run nightly also for 5.0.0 branch

* Automated dotnet-format update (#14581)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Ordering children while adding (#8231)

* Ordering children while adding

* Abstract OrderElement

* Re-order on runtime child addition and Tests

* Runtime addition ensures Z Index

* Stop performance measurement on return

* Update VisualElementPackager.cs

Co-authored-by: Gerald Versluis <gerald@verslu.is>
Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>

* If stroke is null avoid render a shape stroke (#14587)

* Fix broken Android platform tests (#14590)

* Automated dotnet-format update (#14595)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* [macOS] Fix RadioButton activated behaviour (#14139)

* Respond to Activated event

* Set initial control state based on element state

* Revert "Set initial control state based on element state"

This reverts commit 1bd1238.

* CollectionView RemainingItemsThreshold implementation for UWP (#14202)

* [Android] Fix building with Android stable bits (#14608)

* Set java sdk path

* Use class-parse

Co-authored-by: Braini01 <T_Dittmar@web.de>
Co-authored-by: Tim Dittmar <Tim.Dittmar@Exxeta.com>
Co-authored-by: Rachel Kang <rachel.j.kang@gmail.com>
Co-authored-by: Rachel Kang <rachelkang@microsoft.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: MH Rastegari <42671084+mhrastegary77@users.noreply.github.com>
Co-authored-by: Felipe Silveira <felipe@transis.com.br>
Co-authored-by: Rui Marinho <me@ruimarinho.net>
Co-authored-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Thomas Mijieux <tmijieux@users.noreply.github.com>
Co-authored-by: E.Z. Hart <hartez@users.noreply.github.com>
Co-authored-by: Marco Goertz <mgoertz@microsoft.com>
Co-authored-by: Kirill <rotorgames@bk.ru>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: Lee Millward <lee.millward@gmail.com>
Co-authored-by: Gerald Versluis <gerald@verslu.is>
Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Co-authored-by: Filip Navara <filip.navara@gmail.com>
Co-authored-by: Shanmukha Ranganath <shanmukharanganath@gmail.com>
Co-authored-by: Julio César Rocha <JunielKatarn@users.noreply.github.com>
Co-authored-by: nk221 <kirill.n@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/entry e/2 🕑 2 in-progress This issue has an associated pull request that may resolve it! p/Android t/bug 🐛
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants