Skip to content

Commit

Permalink
fix: TextInput npe in #29452 on react native side (#37302)
Browse files Browse the repository at this point in the history
Summary:
Fix the TextInput npe in #29452 on react native side because if it is just avoided on App side by  changing themes may cause side effects.

## Changelog:

- [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: #37302

Test Plan:
Thanks to Fabriziobertoglio1987's wok, the problem in #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 facebook-github-bot committed May 23, 2023
1 parent c56968c commit 98789e9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 38 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.bridge.ReadableMap;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -263,6 +265,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);
}
}
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.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
</style>

</resources>

0 comments on commit 98789e9

Please sign in to comment.