androidx requestPermissionLauncher 导致 java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
androidx requestPermissionLauncher causes java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
我正在关注如何实现权限的 Android docs example,它建议使用 androidx
的 RequestPermission
和 requestPermissionLauncher
API 来:
allow the system to manage the request code that's associated with a permissions request
我基本上复制了示例代码:
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
)
}
}
但是在 运行 之后,我得到一个
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
包含
的行出错
requestPermissionLauncher.launch(
堆栈跟踪:
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.onLaunch(ComponentActivity.java:192)
at androidx.activity.result.ActivityResultRegistry.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)
我真的想不通。我添加了 androidx gradle 依赖项
添加此依赖项以解决问题
implementation 'androidx.fragment:fragment:1.3.4'
我正在关注如何实现权限的 Android docs example,它建议使用 androidx
的 RequestPermission
和 requestPermissionLauncher
API 来:
allow the system to manage the request code that's associated with a permissions request
我基本上复制了示例代码:
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
)
}
}
但是在 运行 之后,我得到一个
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
包含
的行出错requestPermissionLauncher.launch(
堆栈跟踪:
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.onLaunch(ComponentActivity.java:192)
at androidx.activity.result.ActivityResultRegistry.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)
我真的想不通。我添加了 androidx gradle 依赖项
添加此依赖项以解决问题
implementation 'androidx.fragment:fragment:1.3.4'