[Android] Tried to use permissions API while not attached to an Activity.

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:25 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
manishoocommented, Oct 23, 2017

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

7reactions
mannolcommented, Aug 5, 2017

Still present on 0.47

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tried to use permissions API while not attached to an activity
I was able to figure it out by asking for permission. I solved it with this code below: checkAndroidPermission = async () =>...
Read more >
Tried to use permissions API while not attached to an activity ...
Coding example for the question Tried to use permissions API while not attached to an activity-React Native.
Read more >
Request app permissions - Android Developers
To do this, use the RequestPermission contract, included in an AndroidX library, where you allow the system to manage the permission request code...
Read more >
Change app permissions on your Android phone
Change app permissions · All the time (location only): The app can use the permission at any time, even when you're not using...
Read more >
Troubleshooting React-Native - OneSignal Documentation
If you have detached from Expo or CRNA, you might need to fix versions of the Google Play Services that this library is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found