Android — ActivityResultLauncher Crash —遇到 Can only use lower 16 bits for requestCode 怎麼辦?

JLin
Nov 17, 2021

如果你使用

registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){
//...
}

來索取權限,但明明requestCode定義阿,為什麼他會丟出

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

原來你必須同時include

implementation 'androidx.activity:activity-ktx:1.3.1'
implementation 'androidx.fragment:fragment-ktx:1.3.1'

如果缺一個,系統很奇怪的會拋出剛剛上面的那個錯誤,當我只有使用

implementation 'androidx.activity:activity-ktx:1.3.1'

就會有剛剛那個錯誤,所以加上了

implementation 'androidx.activity:activity-ktx:1.3.1'
implementation 'androidx.fragment:fragment-ktx:1.3.1'

就沒問題了,一個神奇的bug , done.

參考:

https://medium.com/@anum.amin/activityresultlauncher-crash-can-only-use-lower-16-bits-for-requestcode-7a4fc5ea5eb0

--

--

JLin

台中人 @ Taipei , JAVA / Android /Kotlin / Kotlin Native 對於Kotlil衍生的JVM等技術 Compose for web / desktop / Ktor Server 都有小興趣