android
type_error
ai_generated
true
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%Fix Rate
82%Confidence
1Evidence
2024-06-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| AndroidX Activity 1.7.2 | active | — | — | — |
| Android 14 (API 34) | active | — | — | — |
| Kotlin 1.9.0 | active | — | — | — |
Root Cause
ActivityResultContract expects a different input/output type than the one provided in registerForActivityResult.
generic中文
ActivityResultContract 期望的输入/输出类型与 registerForActivityResult 中提供的不匹配。
Workarounds
-
90% success Ensure the contract matches the input type. For Intent, use ActivityResultContracts.StartActivityForResult() and pass Intent explicitly: registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> ... }
Ensure the contract matches the input type. For Intent, use ActivityResultContracts.StartActivityForResult() and pass Intent explicitly: registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> ... } -
85% success 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) }
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) }
中文步骤
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) }
Dead Ends
Common approaches that don't work:
-
Use ActivityResultContracts.StartActivityForResult() without any type casting
80% fail
Still throws same error if Input class is not Intent; contract requires explicit type conversion.
-
Remove the contract entirely and use deprecated startActivityForResult
90% fail
Deprecated and won't compile on targetSdk 34+; also breaks modern lifecycle handling.