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] Tried to use permissions API while not attached to an Activity. #10009

Closed
alexHlebnikov opened this issue Sep 21, 2016 · 25 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@alexHlebnikov
Copy link

alexHlebnikov commented Sep 21, 2016

I've got this error.
I'm using RN Android Permission Module.
Tested on RN 0.33.0 and RN 0.34.0.RC.0.
Problem occurs only on Android devices with API Level 23+ (where Android Permission Module is used).

Here is my code:

    requestPermission = async () => {
        try {
            const granted = await PermissionsAndroid.requestPermission(
                PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
            );

            const { actions } = this.props;
            actions.setGeolocationPermissionStatus(granted);
        } catch (err) {
            /*eslint-disable */
            console.warn(err);
            /*eslint-enable */
        }
    };

    componentDidMount() {
        setTimeout(() => {
            this.requestPermission();
        }, 0);
    }

And here is stack trace:

Exception java.lang.IllegalStateException: Tried to use permissions API while not attached to an Activity.
com.facebook.react.modules.permissions.PermissionsModule.getPermissionAwareActivity (PermissionsModule.java:125)
com.facebook.react.modules.permissions.PermissionsModule.requestPermission (PermissionsModule.java:86)
java.lang.reflect.Method.invoke (Method.java)
com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke (BaseJavaModule.java:319)
com.facebook.react.cxxbridge.JavaModuleWrapper.invoke (JavaModuleWrapper.java:158)
com.facebook.react.bridge.queue.NativeRunnable.run (NativeRunnable.java)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage (MessageQueueThreadHandler.java:31)
android.os.Looper.loop (Looper.java:148)
com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run (MessageQueueThreadImpl.java:196)
java.lang.Thread.run (Thread.java:818)
@alexHlebnikov
Copy link
Author

Same problem on RN 0.35.0.RC.0.

@hey99xx
Copy link

hey99xx commented Sep 27, 2016

Well the stack trace looks pretty clear. Was your React instance manager attached to an activity when you called this or not? It's a plain English error message, I don't see what you are asking here.

@alexHlebnikov
Copy link
Author

@hey99xx how could there be no activity?
I'm requesting permission on componentDidMount() - at that point activity is already exists.

@alexHlebnikov
Copy link
Author

Seems like #10221 could fix the issue. Waiting for review.

facebook-github-bot pushed a commit that referenced this issue Oct 12, 2016
Summary:
We're seeing a lot of crashes from `PermissionsModule` not being able to access the current activity, mentioned in #10009 and here: #9310 (comment)

As far as I can tell, there is no way to ensure the Activity exists since the `ReactContext` holds a `WeakReference` to the current Activity and it appears that the lifecycle calls are happening in the right order (so not the same as #9310).

This will at least allow people to catch the error in JS and update the UI or try again as opposed to crashing the app.

I'm working on some bigger changes in #10221 but this is a smaller change and important to get fixed I think.
Closes #10351

Differential Revision: D4010242

fbshipit-source-id: 7a76973bb2b3e45817d4283917740c89a10ec0b0
@alexHlebnikov
Copy link
Author

alexHlebnikov commented Oct 24, 2016

The problem is still here. I can reproduce it now.

When my app loads for the first time, the permission dialog is shown. If user "pauses" app by clicking on right button on Android or if there is another app appears over my app (message from messenger, for example) and if after that app becomes "unpaused" (resumed), then it crashes at that moment.

I can guess, the problem is somewhat connected with reactContext and with the way the app becomes resumed after onPause.

grabbou pushed a commit that referenced this issue Oct 25, 2016
Summary:
We're seeing a lot of crashes from `PermissionsModule` not being able to access the current activity, mentioned in #10009 and here: #9310 (comment)

As far as I can tell, there is no way to ensure the Activity exists since the `ReactContext` holds a `WeakReference` to the current Activity and it appears that the lifecycle calls are happening in the right order (so not the same as #9310).

This will at least allow people to catch the error in JS and update the UI or try again as opposed to crashing the app.

I'm working on some bigger changes in #10221 but this is a smaller change and important to get fixed I think.
Closes #10351

Differential Revision: D4010242

fbshipit-source-id: 7a76973bb2b3e45817d4283917740c89a10ec0b0
@alexHlebnikov
Copy link
Author

@grabbou Hi! Fix for this problem (#10351) doesn't helped. I've write above how to reproduce this issue. It could be great if you can check my guess.

facebook-github-bot pushed a commit that referenced this issue Nov 25, 2016
…sions

Summary:
In order to get featured in the Google Play Store, we had to handle a few specific cases with permissions based on feedback from the editorial team.

First, which was previously possible with this permissions module was bumping the sdk to version 23.

The second is requesting multiple permissions at one time. In order for the camera + upload to work, we needed to request both camera permissions + media storage in one flow.

The last is handling the case where the user checks the "Never Ask Again" box. This will only appear after a user denies a permission once and is then prompted again. The logic for handling this case is taken from here: http://stackoverflow.com/questions/31928868/how-do-we-distinguish-never-asked-from-stop-asking-in-android-ms-runtime-permis/35495372#35495372

We were also seeing a few crashes similar to #10009 due to `onRequestPermissionsResult` being called before `onResume` (http://stackoverflow.com/questions/35205643/why-is-onresume-called-after-onrequestpermissionsresult), so I delaye
Closes #10221

Differential Revision: D4232551

fbshipit-source-id: fee698d1c48a2d86623cb87996f3d17f4c10a62e
DanielMSchmidt pushed a commit to DanielMSchmidt/react-native that referenced this issue Jan 4, 2017
…sions

Summary:
In order to get featured in the Google Play Store, we had to handle a few specific cases with permissions based on feedback from the editorial team.

First, which was previously possible with this permissions module was bumping the sdk to version 23.

The second is requesting multiple permissions at one time. In order for the camera + upload to work, we needed to request both camera permissions + media storage in one flow.

The last is handling the case where the user checks the "Never Ask Again" box. This will only appear after a user denies a permission once and is then prompted again. The logic for handling this case is taken from here: http://stackoverflow.com/questions/31928868/how-do-we-distinguish-never-asked-from-stop-asking-in-android-ms-runtime-permis/35495372#35495372

We were also seeing a few crashes similar to facebook#10009 due to `onRequestPermissionsResult` being called before `onResume` (http://stackoverflow.com/questions/35205643/why-is-onresume-called-after-onrequestpermissionsresult), so I delaye
Closes facebook#10221

Differential Revision: D4232551

fbshipit-source-id: fee698d1c48a2d86623cb87996f3d17f4c10a62e
@felipemartim
Copy link

I was having the same problem as I was requesting the permission as soon as the Activity was created. Delaying the permission request a bit solved it for me.

@hramos hramos added the Icebox label Jul 20, 2017
@hramos
Copy link
Contributor

hramos commented Jul 20, 2017

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

@hramos hramos closed this as completed Jul 20, 2017
@ujwal-setlur
Copy link

I see this on a real device with react-native 0.47. Didn't see it on an emulator.

@mannol
Copy link
Contributor

mannol commented Aug 5, 2017

Still present on 0.47

@kontinuity
Copy link

I think this happens when the app is in the background while starting or requesting permissions and may only happen in dev mode.

@mannol
Copy link
Contributor

mannol commented Aug 7, 2017

This happens to me while using react-native-navigation in release mode. It's probably related to RNN but I'm not sure.

@xhou
Copy link

xhou commented Sep 21, 2017

I got the same issue to @mannol
I am using

[email protected]
wix/[email protected]

The crash only happens after a fresh install of my app (delete my app and install it). It works well if I turn off the location permission of my app in settings and reopen my app.

Here is my adb logcat result:

9-21 16:12:22.598   609   622 I GnssLocationProvider: WakeLock released by handleMessage(3, 0, com.android.server.location.GnssLocationProvider$GpsRequest@4ed4899)
09-21 16:12:22.616  4371  4371 D AndroidRuntime: Shutting down VM
09-21 16:12:22.617  4371  4371 E AndroidRuntime: FATAL EXCEPTION: main
09-21 16:12:22.617  4371  4371 E AndroidRuntime: Process: com.mycompany.maycompany, PID: 4371
09-21 16:12:22.617  4371  4371 E AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=0, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.limebike.limebikeops/com.reactnativenavigation.controllers.NavigationActivity}: java.lang.IllegalStateException: Tried to use permissions API while not attached to an Activity.
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.ActivityThread.-wrap20(ActivityThread.java)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:154)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6119)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Tried to use permissions API while not attached to an Activity.
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at com.facebook.react.modules.permissions.PermissionsModule.getPermissionAwareActivity(PermissionsModule.java:215)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at com.facebook.react.modules.permissions.PermissionsModule.onRequestPermissionsResult(PermissionsModule.java:207)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at com.reactnativenavigation.controllers.NavigationActivity.onRequestPermissionsResult(NavigationActivity.java:442)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7084)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.Activity.dispatchActivityResult(Activity.java:6936)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
09-21 16:12:22.617  4371  4371 E AndroidRuntime: 	... 9 more
09-21 16:12:22.667   609   823 W ActivityManager:   Force finishing activity com.limebike.limebikeops/com.reactnativenavigation.controllers.NavigationActivity
09-21 16:12:22.696  2164  2178 D OpenGLRenderer: endAllActiveAnimators on 0xcf5b5d00 (RippleDrawable) with handle 0xcf5ccb60
09-21 16:12:22.748   341  2546 D gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-21 16:12:22.751   341  2546 D gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-21 16:12:22.754   341  2546 D gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-21 16:12:22.758   609  2356 I OpenGLRenderer: Initialized EGL, version 1.4
09-21 16:12:22.758   609  2356 D OpenGLRenderer: Swap behavior 1
09-21 16:12:22.779   609  2356 E EGL_emulation: tid 2356: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
09-21 16:12:22.779   609  2356 W OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xcec9fba0, error=EGL_BAD_MATCH
09-21 16:12:23.196   609   623 W ActivityManager: Activity pause timeout for ActivityRecord{9d5c438 u0 com.mycompany.mycompany/com.reactnativenavigation.controllers.NavigationActivity t231 f}
09-21 16:12:23.248   341   606 D gralloc_vbox86: gralloc_alloc: format 1 and usage 0x333 imply creation of host color buffer
09-21 16:12:23.267   341   341 E EGL_emulation: tid 341: eglCreateSyncKHR(1669): error 0x3004 (EGL_BAD_ATTRIBUTE)
09-21 16:12:23.475   341   345 D gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-21 16:12:23.480   341   345 D gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-21 16:12:23.485   341   345 D gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
09-21 16:12:23.499  1151  1245 E EGL_emulation: tid 1245: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
09-21 16:12:23.499  1151  1245 W OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xcf8a7500, error=EGL_BAD_MATCH
09-21 16:12:23.510   341   341 W SurfaceFlinger: couldn't log to binary event log: overflow.
09-21 16:12:23.642  4000  4009 W SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/metrics.db.16' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
09-21 16:12:23.644  4000  4009 W SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/help_responses.db.18' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
09-21 16:12:23.646  4000  4009 W SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked!  Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

@manishoo
Copy link

I'm having this issue as well, why is this issue closed?

@thevolcanomanishere
Copy link

Same here

@Moreno97
Copy link

Same here...

@thevolcanomanishere
Copy link

thevolcanomanishere commented Nov 29, 2017 via email

@Moreno97
Copy link

@thevolcanomanishere using last RN version that supports Oreo and error stills there. Probably is something related to react-native-navigation??

@thevolcanomanishere
Copy link

thevolcanomanishere commented Nov 29, 2017

@Moreno97 Sorry I was thinking of a separate problem. It was RNN. I removed it and it went away. RNN with the react native debug overlay doesn't request permissions properly in line with Androids new permission model for drawing over the screen.

@Moreno97
Copy link

@thevolcanomanishere so the problem is RNN. I suppose there's no fix for that, true?

@thevolcanomanishere
Copy link

thevolcanomanishere commented Nov 29, 2017 via email

@Moreno97
Copy link

@thevolcanomanishere that was already implemented on previous versions in RNN. Probably a quick fix is using setTimeout on componentDidMount.

@brunolemos
Copy link
Contributor

Anyone have a fix or workaround?

java.lang.RuntimeException · Failure delivering result ResultInfo{who=@android:requestPermissions:, request=0, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) }} to activity {com.minderapps.minder/com.reactnativenavigation.controllers.NavigationActivity}: java.lang.IllegalStateException: Tried to use permissions API while not attached to an Activity.

I'm requesting permissions after the screen is visible so I don't see why this happens.

react-native 0.54
react-native-navigation 1.1.365

@brunolemos
Copy link
Contributor

This may fix: wix/react-native-navigation#2043 (comment)

@anwarhamr
Copy link

@Moreno97 setTimeout worked for me but what a hack. I'm not using anything special in React-native-navigation so I should just dump it since it's giving me such pains...

@facebook facebook locked as resolved and limited conversation to collaborators Jul 20, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests