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

[Android] Fix to Issue Java.Lang.IndexOutOfBoundsException: setSpan #12764

Merged
merged 4 commits into from
Jul 8, 2021
Merged

[Android] Fix to Issue Java.Lang.IndexOutOfBoundsException: setSpan #12764

merged 4 commits into from
Jul 8, 2021

Conversation

Transis-Felipe
Copy link
Contributor

@Transis-Felipe Transis-Felipe commented Nov 6, 2020

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

Fix to Issue #12762
Java.Lang.IndexOutOfBoundsException: setSpan (nn ... nn) ends beyond length n-1
@Transis-Felipe Transis-Felipe changed the title Update EntryRenderer.cs Fix to Issue #12762 [Bug] Java.Lang.IndexOutOfBoundsException: setSpan Nov 6, 2020
@Transis-Felipe Transis-Felipe changed the title Fix to Issue #12762 [Bug] Java.Lang.IndexOutOfBoundsException: setSpan [Android] Fix to Issue Java.Lang.IndexOutOfBoundsException: setSpan Nov 6, 2020
@rmarinho
Copy link
Member

rmarinho commented Nov 6, 2020

I m not sure this is the right fix.. Maybe we need something like this:

void UpdateText()
{
	if (EditText == null || Element == null)
		return;

	var text = Element.UpdateFormsText(Element.Text, Element.TextTransform);

	if (EditText.Text == text)
		return;

	EditText.Text = text;
	if (EditText.IsFocused)
	{
		EditText.SetSelection(EditText.Text.Length);
		EditText.ShowKeyboard();
	}
}

@rmarinho rmarinho linked an issue Nov 6, 2020 that may be closed by this pull request
@Transis-Felipe
Copy link
Contributor Author

Transis-Felipe commented Nov 7, 2020

@rmarinho Yes, your solution is better.
Just to learn: You set the text to entry and the control will limit the size of string. So, it will never have an selection bigger than the size of the field, right?

Do you want to create an PR or I can change mine with your suggestion?

@rmarinho
Copy link
Member

rmarinho commented Nov 8, 2020

@transis2 yes that's my idea, or with the check above if EditText is null (was disposed) we just return to also make it safer.

I tried to reproduce this issue with a number ways but wasn't able, but that's the only idea i have right now to try to fix it.
You can update your PR.

@Transis-Felipe
Copy link
Contributor Author

Transis-Felipe commented Nov 9, 2020

@rmarinho Update the PR with your suggestion.

Thanks!

@Transis-Felipe
Copy link
Contributor Author

Transis-Felipe commented Nov 11, 2020

Hi @rmarinho

Our PR is showing that some checks were not successful.
May I create a new one from the latest XF version ?

@rmarinho
Copy link
Member

@transis2 i think i fixed the issue, running a new build and tests

@felipemomm
Copy link
Contributor

felipemomm commented Apr 21, 2021

@rmarinho When is it going to be merged? I am facing the same issue in my project, so I had to implement my own UpdateText method on OnElementPropertyChanged to avoid it (XF 4.8).

Basically, I am receiving a text longer than the Entry's maxLen from the backend, it shows only up to the maxLen, however when I tap in the field to edit the text, it crashes.

@nrmiller
Copy link

nrmiller commented Jul 8, 2021

@rmarinho Is this PR still being considered? It looks like it has been around for awhile, and I'm running into this error as late as Xamarin.Forms 5.0.0.2021 on a Google Pixel 3 XL

My scenario to trigger this bug with 100% reliability:

  1. Set Entry.MaxLength to 5 via XAML
  2. Enter "3910" with numeric keyboard.
  3. Without dismissing the keyboard, tap a radio button* to trigger setting the text to "1539.37" via a view-model property.

*Specifically, I have two radio buttons (1 for centimeters and 1 for inches). The goal is to convert what the user enters upon changing the select radio button. Because converting 3910cm yields 1539.37in and because "1539.37" > 5 characters, the crash occurs.
I tried to trigger this issue by creating a simple button and setting the Text to "000000" in the bound view-model command. This did not work, so I'm assuming something tricky is happening specifically with triggering via RadioButtonGroup.SelectedValue. Perhaps this is related to how MessagingCenter is used under the hood?

FWIW, here's the stacktrace from the Xamarin Forms side (I'm stilling getting the IndexOutOfBounds on the Android side):

TargetInvocationException: Exception has been thrown by the target of an invocation.
      at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00086] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <98fdeeb5cad34f67b78b105df850970d>:0 
      at Xamarin.Forms.MessagingCenter+Subscription.InvokeCallback (System.Object sender, System.Object args) [0x00076] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.MessagingCenter.InnerSend (System.String message, System.Type senderType, System.Type argType, System.Object sender, System.Object args) [0x0006b] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.MessagingCenter.Xamarin.Forms.IMessagingCenter.Send[TSender,TArgs] (TSender sender, System.String message, TArgs args) [0x00013] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.MessagingCenter.Send[TSender,TArgs] (TSender sender, System.String message, TArgs args) [0x00005] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.RadioButtonGroup.UpdateRadioButtonGroup (Xamarin.Forms.RadioButton radioButton) [0x00028] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.RadioButton.OnIsCheckedPropertyChanged (System.Boolean isChecked) [0x00003] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.RadioButton+<>c.<.cctor>b__116_1 (Xamarin.Forms.BindableObject b, System.Object o, System.Object n) [0x00000] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.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) [0x0012a] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.RadioButton.set_IsChecked (System.Boolean value) [0x00000] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.RadioButton.SelectRadioButton (System.Object sender, System.EventArgs e) [0x00008] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.TapGestureRecognizer.SendTapped (Xamarin.Forms.View sender) [0x0003b] in <978ec34c5c9c4c4eb3d73b6da958bcd6>:0 
      at Xamarin.Forms.Platform.Android.TapGestureHandler.OnTap (System.Int32 count, Xamarin.Forms.Point point) [0x000a2] in <04c545f414d24a37af95d995791bb9a9>:0 
      at Xamarin.Forms.Platform.Android.InnerGestureListener.Android.Views.GestureDetector.IOnGestureListener.OnSingleTapUp (Android.Views.MotionEvent e) [0x0002e] in <04c545f414d24a37af95d995791bb9a9>:0 
      at Android.Views.GestureDetector+IOnGestureListenerInvoker.n_OnSingleTapUp_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_e) [0x0000f] in <cdf69449759145adbc699e96b2eb3764>:0 
        at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.140(intptr,intptr,intptr)

@rmarinho rmarinho merged commit 163f4fc into xamarin:5.0.0 Jul 8, 2021
rmarinho added a commit that referenced this pull request 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Java.Lang.IndexOutOfBoundsException: setSpan
6 participants