permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.21k stars 1.44k forks source link

Fatal Exception: java.lang.IllegalStateException #721

Closed flobizDipen closed 3 years ago

flobizDipen commented 3 years ago

Description

This is happening because hasSelfPermissions requires activity context and for that, you guys are using requireActivity() which can be null too

@NonNull public final FragmentActivity requireActivity() { FragmentActivity activity = getActivity(); if (activity == null) { throw new IllegalStateException("Fragment " + this + " not attached to an activity."); } return activity; }

Environment

hotchemi commented 3 years ago

thx for the report. But I'm afraid you should avoid calling functionWithPermissionCheck when activity could be null because then we can't do anything(PermissionChecker.checkSelfPermission requires context and we eventually need activity or context).

rq973017553 commented 1 year ago

please use "by lazy" in your fragment private val showCamera by lazy { constructPermissionsRequest( permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE ), onShowRationale = ::onCameraShowRationale, onPermissionDenied = ::onCameraDenied, onNeverAskAgain = ::onCameraNeverAskAgain ) { startActivity(Intent(mActivity, XXXX::class.java)) } }