Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: VolumeManagerModule setupKeyListener NullPointerException error #32

Open
arekm213 opened this issue Feb 15, 2024 · 4 comments
Open

Comments

@arekm213
Copy link

arekm213 commented Feb 15, 2024

We are getting a lot of error reports like this in our app which uses your library:

Exception java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window android.app.Activity.getWindow()' on a null object reference
  at com.reactnativevolumemanager.VolumeManagerModule.lambda$setupKeyListener$1$com-reactnativevolumemanager-VolumeManagerModule (VolumeManagerModule.java:90)
  at com.reactnativevolumemanager.VolumeManagerModule$$ExternalSyntheticLambda2.run
  at android.os.Handler.handleCallback (Handler.java:958)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:205)
  at android.os.Looper.loop (Looper.java:294)
  at android.app.ActivityThread.main (ActivityThread.java:8248)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:552)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:971)

Could you please think about proper fix for this?
We are currently early returning in setupKeyListener method if mContext.getCurrentActivity() returns null, but this probably could be handled better
This happens on "react-native": "0.68.7", unfortunately im not able to reproduce this issue myself

@hgapsilva
Copy link

in the file a/node_modules/react-native-volume-manager/android/src/main/java/com/reactnativevolumemanager/VolumeManagerModule.java b/node_modules/react-native-volume-manager/android/src/main/java/com/reactnativevolumemanager/VolumeManagerModule.java , in the setupKeyListener method you can add a condition to see if the mContext exists, like this:

private void setupKeyListener() {
     runOnUiThread(() -> {

       if (mContext.getCurrentActivity() == null) return;

       View rootView =
       ... 
}

While you won't have this fix in the lib, you can make this change and then use this command in your project's terminal:

npx patch-package react-native-volume-manager

this will create a patch with the changes (the file shouldn't be large when created, it should only have the changes) and whenever you install node_modules it will use this patch to make your change.

this error is similar to this one:

#28

@anirudhsama
Copy link

We are also seeing a lot of crashes due to this. Any chance it can be merged @hirbod ?

@hirbod
Copy link
Owner

hirbod commented Jul 17, 2024

I'll tackle this soon, I have lots of private things going on lately, sorry for the delay.

@anirudhsama
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants