android
type_error
ai_generated
true
java.lang.IllegalArgumentException: 无法将值转换为 Intent,类 android.content.Intent 不是有效类型
java.lang.IllegalArgumentException: Can't convert value to Intent, class android.content.Intent is not a valid type
ID: android/activityresult-api-contract-mismatch
87%修复率
82%置信度
1证据数
2024-06-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| AndroidX Activity 1.7.2 | active | — | — | — |
| Android 14 (API 34) | active | — | — | — |
| Kotlin 1.9.0 | active | — | — | — |
根因分析
ActivityResultContract 期望的输入/输出类型与 registerForActivityResult 中提供的不匹配。
English
ActivityResultContract expects a different input/output type than the one provided in registerForActivityResult.
解决方案
-
Ensure the contract matches the input type. For Intent, use ActivityResultContracts.StartActivityForResult() and pass Intent explicitly: registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> ... } -
If using custom contract, override createIntent correctly. Example: class CustomContract : ActivityResultContract<Uri, Boolean>() { override fun createIntent(context: Context, input: Uri) = Intent(Intent.ACTION_VIEW, input) }
无效尝试
常见但无效的做法:
-
Use ActivityResultContracts.StartActivityForResult() without any type casting
80% 失败
Still throws same error if Input class is not Intent; contract requires explicit type conversion.
-
Remove the contract entirely and use deprecated startActivityForResult
90% 失败
Deprecated and won't compile on targetSdk 34+; also breaks modern lifecycle handling.