Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

androidx requestPermissionLauncher causes java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode

I'm following the Android docs example on how to implement permissions, and it suggested using androidx's RequestPermission and requestPermissionLauncher APIs to:

allow the system to manage the request code that's associated with a permissions request


I've essentially copied the sample code:

private fun checkForPermissions() {

    // setup permission callback
    val requestPermissionLauncher =
        registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
            if (isGranted) {
                // success
                Log.d("RPMT", "success")
            } else {
                Log.d("RPMT", "failure")
                // failure
            }
        }

    // check permission
    if (ContextCompat.checkSelfPermission(
            this@MainActivity,
            Manifest.permission.CAMERA
        ) == PackageManager.PERMISSION_GRANTED
    ) {
        Log.d("RPMT", "carry on with flow")
    } else {
        requestPermissionLauncher.launch(
            Manifest.permission.CAMERA
        )
    }
}

however upon running this, I get a

java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode

error at the line containing

requestPermissionLauncher.launch(

The stack trace:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{no.dwb.rpmt_kotlin_0/no.dwb.rpmt_kotlin_0.MainActivity}: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6923)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
...
     Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
        at androidx.fragment.app.FragmentActivity.checkForValidRequestCode(FragmentActivity.java:715)
        at androidx.fragment.app.FragmentActivity.validateRequestPermissionsRequestCode(FragmentActivity.java:730)
        at androidx.core.app.ActivityCompat.requestPermissions(ActivityCompat.java:500)
        at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:192)
        at androidx.activity.result.ActivityResultRegistry$3.launch(ActivityResultRegistry.java:160)
        at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:42)
        at no.dwb.rpmt_kotlin_0.MainActivity.checkForPermissions(MainActivity.kt:40)
        at no.dwb.rpmt_kotlin_0.MainActivity.onCreate(MainActivity.kt:16)

I seriously cannot figure this out. I've added the androidx gradle dependency

like image 445
dantechguy Avatar asked Oct 13 '20 13:10

dantechguy


1 Answers

Add this dependency to fix the issue

implementation 'androidx.fragment:fragment:1.3.4'
like image 65
Hayssam Soussi Avatar answered Oct 11 '22 21:10

Hayssam Soussi