kadiraydinli / react-native-system-navigation-bar

React Native lets you customize the navigation bar for Android.
https://www.npmjs.com/package/react-native-system-navigation-bar
MIT License
270 stars 19 forks source link

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

Open SanKlein opened 10 months ago

SanKlein commented 10 months ago

Description

Hi, thank you for the great library. Only way I was able to set the theme in the Android app status bar and navigation bar.

I am running into this crash lately.

I am calling this in my code: SystemNavigationBar && SystemNavigationBar.setBarMode && SystemNavigationBar.setBarMode(isDarkMode ? 'light' : 'dark');

I added the checks because I wanted to see if that would fix it, but it did not.

Here is the crash I am seeing:

Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference com.reactnativesystemnavigationbar.SystemNavigationBarModule.setModeStyle (SystemNavigationBarModule.java:472) com.reactnativesystemnavigationbar.SystemNavigationBarModule.lambda$setModeStyle$8$SystemNavigationBarModule (SystemNavigationBarModule.java:521) com.reactnativesystemnavigationbar.-$$Lambda$SystemNavigationBarModule$7c9Wq1aEzZlZVxbqz80nWTlUwis.run (Unknown Source:8) android.os.Handler.handleCallback (Handler.java:942)

I am not able to reproduce the issue, but I am seeing it in crashlytics.

Here are some of the devices it is happening on: Android 14 Pixel 6 Android 14 Galaxy S22 Android 13 Oppo Reno8 5G Android 11 Realme C3 Android 10 Galaxy Note9 Android 10 Note3

Please let me know if there is anything else that would be helpful. Thank you!

react-native-system-navigation-bar version

2.6.3

React Native version

0.62.2

Snack, code example, screenshot, or link to a repository

import SystemNavigationBar from 'react-native-system-navigation-bar';

SystemNavigationBar && SystemNavigationBar.setBarMode && SystemNavigationBar.setBarMode(isDarkMode ? 'light' : 'dark');

kadiraydinli commented 9 months ago

Hi @SanKlein, I released a new version. Can you check again in the latest version?

efstathiosntonas commented 8 months ago

@kadiraydinli Hi, it still happens on version 2.6.4, the crashes I got are from Android 11,12,13,14.

stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference
        at com.reactnativesystemnavigationbar.SystemNavigationBarModule.setModeStyle(SystemNavigationBarModule.java:474)
        at com.reactnativesystemnavigationbar.SystemNavigationBarModule.lambda$setNavigationColor$1(SystemNavigationBarModule.java:260)
        at com.reactnativesystemnavigationbar.SystemNavigationBarModule.$r8$lambda$2vOhle1R4mCvq8Qvlxo7ugiWWaI(Unknown)
        at com.reactnativesystemnavigationbar.SystemNavigationBarModule$$ExternalSyntheticLambda4.run(Unknown:12)
        at android.os.Handler.handleCallback(Handler.java:958)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:230)
        at android.os.Looper.loop(Looper.java:319)
        at android.app.ActivityThread.main(ActivityThread.java:8893)
        at java.lang.reflect.Method.invoke(Method.java:-2)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

maybe something like this could get around the crash:

  private void setModeStyle(Boolean light, Integer bar) {
    Activity currentActivity = getCurrentActivity();
    if (currentActivity == null) {
      return;
    }

    int visibility = 0;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    ...
HarshitMadhav commented 4 months ago

Normally, I replicated the issue and then fixed it and raised the PR, this issue should not arise. I think the fix by @efstathiosntonas will work