React-native: [Android] Tried to use permissions API while not attached to an Activity.

Created on 21 Sep 2016  Â·  25Comments  Â·  Source: facebook/react-native

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)
Locked

Most helpful comment

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

All 25 comments

Same problem on RN 0.35.0.RC.0.

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.

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

Seems like https://github.com/facebook/react-native/pull/10221 could fix the issue. Waiting for review.

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 Hi! Fix for this problem (https://github.com/facebook/react-native/pull/10351) doesn't helped. I've write above how to reproduce this issue. It could be great if you can check my guess.

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.

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.

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

Still present on 0.47

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

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

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.

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

Same here

Same here...

Turned out it was problems with Oreo. Use the latest RN

On 29 Nov 2017 8:26 a.m., "Antonio Moreno Valls" notifications@github.com
wrote:

Same here...

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/facebook/react-native/issues/10009#issuecomment-347786581,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJKw_RT3OUwIAJKFiX0CQP3abDJUld4Uks5s7RUogaJpZM4KCaR7
.

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

@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.

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

I read somewhere about implementing PermissionAwareActivity but just gave
up and reverted to my old navigation.

Regards,
Alex McGonagle

On Wed, Nov 29, 2017 at 11:12 AM, Antonio Moreno Valls <
[email protected]> wrote:

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

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/facebook/react-native/issues/10009#issuecomment-347828541,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJKw_fjIg5TJ2URO1qVEHForKKIzjylCks5s7TwJgaJpZM4KCaR7
.

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

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

@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...

Was this page helpful?
0 / 5 - 0 ratings