Skip to content

Commit e62b57e

Browse files
luluwu2032kelset
authored andcommitted
Fix Xiaomi TextInput crash in native
Summary: Long term fix in native for Error: android_crash:java.lang.NullPointerException:android.widget.Editor$SelectionModifierCursorController.access$300 For more detail please see T68183343 D23301714 Changelog: [Android][Changed] - Fix Xiaomi TextInput crash in native Reviewed By: mdvacca Differential Revision: D23331828 fbshipit-source-id: 914f2d431772f49711b940d47a2b3ef57ab82cdc
1 parent c72ecdb commit e62b57e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ export type Props = $ReadOnly<{|
488488
* The following values work on Android only:
489489
*
490490
* - `visible-password`
491+
*
492+
* On Android devices manufactured by Xiaomi with Android Q, 'email-address'
493+
* type will be replaced in native by 'default' to prevent a system related crash.
491494
*/
492495
keyboardType?: ?KeyboardType,
493496

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,25 @@ public String getReturnKeyType() {
383383
@Override
384384
public void setInputType(int type) {
385385
Typeface tf = super.getTypeface();
386-
super.setInputType(type);
387-
mStagedInputType = type;
388386
// Input type password defaults to monospace font, so we need to re-apply the font
389387
super.setTypeface(tf);
390388

389+
int inputType = type;
390+
391+
// Set InputType to TYPE_CLASS_TEXT (the default one for Android) to fix a crash on Xiaomi
392+
// devices with Android Q. This crash happens when focusing on a email EditText within a
393+
// ScrollView, a prompt will be triggered but the system fail to locate it properly.
394+
// Here is an example post discussing about this issue:
395+
// https://github.com/facebook/react-native/issues/27204
396+
if (inputType == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
397+
&& Build.VERSION.SDK_INT == Build.VERSION_CODES.Q
398+
&& Build.MANUFACTURER.startsWith("Xiaomi")) {
399+
inputType = InputType.TYPE_CLASS_TEXT;
400+
}
401+
402+
super.setInputType(inputType);
403+
mStagedInputType = inputType;
404+
391405
/**
392406
* If set forces multiline on input, because of a restriction on Android source that enables
393407
* multiline only for inputs of type Text and Multiline on method {@link
@@ -401,7 +415,7 @@ public void setInputType(int type) {
401415
// We override the KeyListener so that all keys on the soft input keyboard as well as hardware
402416
// keyboards work. Some KeyListeners like DigitsKeyListener will display the keyboard but not
403417
// accept all input from it
404-
mKeyListener.setInputType(type);
418+
mKeyListener.setInputType(inputType);
405419
setKeyListener(mKeyListener);
406420
}
407421

0 commit comments

Comments
 (0)