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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. Ensure the contract matches the input type. For Intent, use ActivityResultContracts.StartActivityForResult() and pass Intent explicitly: registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> ... }
  2. 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) }

无效尝试

常见但无效的做法:

  1. Use ActivityResultContracts.StartActivityForResult() without any type casting 80% 失败

    Still throws same error if Input class is not Intent; contract requires explicit type conversion.

  2. Remove the contract entirely and use deprecated startActivityForResult 90% 失败

    Deprecated and won't compile on targetSdk 34+; also breaks modern lifecycle handling.