react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

method 'android.view.Window android.app.Activity.getWindow()' on a null object reference

Open TomCorvus opened this issue 1 year ago • 4 comments

Description

Hi 👋

I have an error on some Android devices since I use useAnimatedKeyboard hook from react-native-reanimated. This fatal crash seems to not appear each time.

Here the error message: Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference com.swmansion.reanimated.keyboard.WindowsInsetsManager.getWindow (WindowsInsetsManager.java:12) causes by

  private Window getWindow() {
    return mReactContext.get().getCurrentActivity().getWindow();
  }

in react-native-reanimated/android/src/main/java/com/swmansion/reanimated/keyboard/WindowsInsetsManager.java L31

It appears that react context is not init and can't get window width yet.

I have saw in react-native core that there is a condition about that to avoid crash (I think)

    Context context = mReactApplicationContext.getCurrentActivity();
    if (context != null) {
      return context;
    }

react-native/ReactAndroid/src/main/java/com/facebook/react/animated/ColorAnimatedNode.java L106

or in react-native-firebase

    // protect against NPEs
    if (currentActivity == null) {
      promise.resolve(null);
      return;
    }

@react-native-firebase/perf/android/src/reactnative/java/io/invertase/firebase/perf/ReactNativeFirebasePerfModule.java L93

If I understand, it needs maybe to get this condition too in WindowsInsetsManager class to avoid this crash.

The reproduction links to react-native-reanimated example because it is difficult to reproduce in production. This crash seems to be "random" but I tried to give as much detail as possible to fix it.

Thanks

Steps to reproduce

  1. Init fresh project
  2. Install react-native-reanimated
  3. Use useAnimatedKeyboard hook

Snack or a link to a repository

https://github.com/software-mansion/react-native-reanimated/tree/main/Example

Reanimated version

3.8.1

React Native version

0.73.6

Platforms

Android

JavaScript runtime

Hermes

Workflow

React Native

Architecture

Paper (Old Architecture)

Build type

Release app & production bundle

Device

Real device

Device model

Galaxy A32 (Android13), Pixel 7 (Android13), Pixel 5 64bit (Android 14)

Acknowledgements

Yes

TomCorvus avatar Apr 12 '24 09:04 TomCorvus

https://github.com/TomCorvus/RNSAV

TomCorvus avatar Apr 12 '24 09:04 TomCorvus

Same here

hoanglongpegaxy avatar Apr 22 '24 06:04 hoanglongpegaxy

I have the same in the production builds

Stack trace from Sentry:

NullPointerException Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference

com.swmansion.reanimated.keyboard.WindowsInsetsManager in getWindow at line 32
com.swmansion.reanimated.keyboard.WindowsInsetsManager in lambda$updateWindowDecor$0$com-swmansion-reanimated-keyboard-WindowsInsetsManager at line 57
com.swmansion.reanimated.keyboard.WindowsInsetsManager$$ExternalSyntheticLambda1 in run at line 4
android.os.Handler in handleCallback at line 938
android.os.Handler in dispatchMessage at line 99
android.os.Looper in loop at line 237
android.app.ActivityThread in main at line 8163
java.lang.reflect.Method in invoke
com.android.internal.os.RuntimeInit$MethodAndArgsCaller in run at line 656
com.android.internal.os.ZygoteInit in main at line 972

Vepsur avatar May 08 '24 08:05 Vepsur

Same. AI report :)

image

RohovDmytro avatar May 13 '24 23:05 RohovDmytro

Hi, I wasn't able to reproduce this issue, but it seems like ongoing issue on react native side: https://github.com/facebook/react-native/issues/37518, https://github.com/facebook/react-native/issues/18345. Would be good if you could test the solution from #6022. The useAnimatedKeyboard hook won't work, but it should not crash the app anymore. Our assumption is that most of the libs handle it this way (error + return), so it must be some corner case when most functionality won't work - that's why we don't try to recover from this state.

maciekstosio avatar May 15 '24 13:05 maciekstosio