-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix Android crash when setting Entry.Text in TextChanged event handler #32143
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com>
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
| namespace Maui.Controls.Sample.Issues | ||
| { | ||
| [Issue(IssueTracker.Github, 32000, "Entry crash when setting Text in TextChanged event handler on Android", PlatformAffected.Android)] | ||
| public class IssueEntryTextChangedCrash : TestContentPage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix mentions EmojiCompat in comments. Consider testing with multi-codepoint emoji sequences like 👨👩👧👦 to ensure Length() handles them correctly.
|
|
||
| // Ensure the selection position doesn't exceed the actual text length | ||
| // to prevent crashes when text is modified by text watchers (e.g., EmojiCompat) | ||
| int selectionPosition = Math.Min(editText.Text?.Length ?? 0, editText.Length()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern Math.Min(editText.Text?.Length ?? 0, editText.Length()) can be simplified. If Text is null, ?? 0 will always result in Min(0, x) = 0, which is correct but verbose.
|
|
||
| // Verify the entry contains "0" after TextChanged handling | ||
| var entryText = App.FindElement(EntryId).GetText(); | ||
| Assert.That(entryText, Is.EqualTo("0"), "Entry should contain '0' after clearing"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test verifies the app doesn't crash and text is correct, but doesn't validate cursor position. After clearing and setting text to "0", the cursor should be at position 1 (end of text).
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description
Fixes a crash on Android when modifying
Entry.Textwithin aTextChangedevent handler. The crash occurred with the error:Root Cause
The issue was a race condition in
EditTextExtensions.UpdateText()whereSetSelection()could be called with a position exceeding the actual text length. This happened because:TextChangedevent fires with the empty textText = "0"to provide a default valueEmojiCompattext watcher is still processing the deletion with the old text lengthSetSelection()is called, the text length has changed, causing an out-of-bounds errorChanges
Added defensive bounds checking to all
SetSelection()calls inEditTextExtensions.cs:UpdateText(EditText, IEntry)- Entry text updatesUpdateText(EditText, IEditor)- Editor text updatesSetInputType()- Cursor position restoration after input type changesThe fix uses
Math.Min()to ensure the selection position never exceeds the current text length:Testing
Added UI test case that reproduces the original crash scenario:
Issues Fixed
Fixes android crash when setting Entry.Text in TextChanged event handler (related to EmojiCompat bounds checking)
Original prompt
This section details on the original issue you should resolve
<issue_title>end should be < than charSequence length</issue_title>
<issue_description>### Description
I am getting a crash when using
TextChangedon aEntrymodifying the value.Steps to Reproduce
Link to public reproduction project repository
https://github.com/jdarwood007/MauiApp9
Version with bug
9.0.110 SR11
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 14
Did you find any workaround?
The problem seems to be entirely calling this:
Relevant log output