今天我切换到新的ResultAPI
,却遇到了这个错误:
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.startActivityForResult(FragmentActivity.java:673)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:207)
at androidx.activity.result.ActivityResultRegistry$3.launch(ActivityResultRegistry.java:147)
at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:42)
at .MainActivity.getVideo(MainActivity.kt:61)
at .MainActivity.access$getVideo(MainActivity.kt:18)
at .MainActivity$onCreate$2.onClick(MainActivity.kt:42)
at android.view.View.performClick(View.java:5232)
at android.view.View$PerformClick.run(View.java:21289)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
当执行以下代码时:
private val takeFile = registerForActivityResult(GetContent()) {
Log.e("MainActivity", "fileName: $it")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
btn_get_video.setOnClickListener {
getVideo()
}
}
private fun getVideo() {
takeFile.launch("video/*")
}
使用这个新的result API,没有办法手动声明REQUEST_CODE
。
注意:
有时可以工作!
[更新]
我注册了多个合同(TakeVideo & GetContent),我发现注册的顺序很重要(第一个像魅力一样工作,但其他人会崩溃)。也许我们只需要为每个活动注册一个合同?!
[更新 #2]
问题已在最新版本的activity(1.2.7-alpha07)中解决。现在它会警告您添加与活动和片段相关的依赖项。
REQUEST_CODE
是由库生成的,而不是开发者。而且它一直生成错误的值。 - user28434'mstep1.2.0-beta01
版本,完全没有使用任何片段! - WindRider