Skip to content

Commit

Permalink
[Upstreamed] fix: TextInput npe in facebook#29452 on react native side (
Browse files Browse the repository at this point in the history
facebook#37302)

Summary:
Fix the TextInput npe in facebook#29452 on react native side because if it is just avoided on App side by  changing themes may cause side effects.

- [ANDROID] [FIXED] - Fix TextInput NPE.

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#37302

Test Plan:
Thanks to Fabriziobertoglio1987's wok, the problem in facebook#29452 can be tested easily. This fixing works fine and causes no side effects.

The following video shows the test result. And the number 101 has been changed to 1001  in RNTester/TextInputKeyProp.js when testing.

https://user-images.githubusercontent.com/23273745/236796702-e61a6fa9-9935-4179-9c5f-e9370d543657.mp4

Reviewed By: javache

Differential Revision: D45688987

Pulled By: cipolleschi

fbshipit-source-id: 4e13c19c10ed53cfcead79e66ab2e232369317e0
  • Loading branch information
jcdhlzq authored and Flewp committed May 16, 2024
1 parent f76c77b commit f1f15fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.R;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.uimanager.Spacing;
Expand Down Expand Up @@ -233,6 +235,13 @@ public void setPadding(int spacingType, float padding) {
* {@code EditText} this class uses to determine the expected size of the view.
*/
protected EditText createInternalEditText() {
return new EditText(getThemedContext());
// By setting a style which has a background drawable, this EditText will have a different
// background drawable instance from that on the UI Thread, which maybe has a default background
// drawable instance.
// Otherwise, DrawableContainer is not a thread safe class, and it caused the npe in #29452.
ContextThemeWrapper context =
new ContextThemeWrapper(
getThemedContext(), R.style.Theme_ReactNative_TextInput_DefaultBackground);
return new EditText(context);
}
}
4 changes: 4 additions & 0 deletions ReactAndroid/src/main/res/shell/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
<item name="android:datePickerMode">calendar</item>
</style>

<style name="Theme.ReactNative.TextInput.DefaultBackground" parent="android:Widget.EditText">
<item name="android:editTextBackground">@drawable/abc_edit_text_material</item>
</style>

</resources>

This file was deleted.

0 comments on commit f1f15fe

Please sign in to comment.