未找到 AndroidX ActivityResultContracts 包/class 未找到
AndroidX ActivityResultContracts package not found / class not found
根据this documentation from Google about launching an activity to get a result:
While the underlying startActivityForResult() and onActivityResult()
APIs are available on the Activity class on all API levels, it is
strongly recommended to use the Activity Result APIs introduced in
AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02.
我想让我的用户在我的应用程序中拍照,并将照片数据传回我的应用程序。我打算使用旧的 startActivityForResult() 但这个新方法看起来会解决很多问题并且更可靠,所以我想试一试。我应该能够调用 registerForActivityResult()
并传递给它一个内置的拍照合同 ActivityResultsContracts.TakePicture:
this.registerForActivityResult(new ActivityResultContracts.TakePicture(), ...);
但我得到:error: package ActivityResultContracts does not exist
我已将此添加到我的 app/build。gradle:
// original include
//implementation 'androidx.appcompat:appcompat:1.1.0'
// suggestion from Google documentation
//implementation 'androidx.appcompat:appcompat:1.2.0-alpha02'
// AndroidStudio suggested a newer version was available
implementation 'androidx.appcompat:appcompat:1.2.0-beta01'
我尝试了 alpha02
和 beta01
,但它们似乎都没有文档中提到的 classes。
当我尝试在 java 文件的顶部手动导入 class 时,AndroidStudio 认为该包也不存在。应该是androidx.activity.result.contract.ActivityResultContracts.TakePicture
,但这是我看到的:
我正在使用 gradle 3.5.3 如果这很重要的话。 (每当我尝试升级到最新的 gradle,我的项目就会变得疯狂,所以我一直使用有效的版本。)
来自引用的文档:
it is strongly recommended to use the Activity Result APIs introduced in AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02.
这些都不在您的依赖项中,至少是您问题中的部分。您一直在操纵 appcompat
,而不是 activity
或 fragment
。
添加其中一个或两个:
implementation "androidx.activity:activity:1.2.0"
implementation "androidx.fragment:fragment:1.3.0"
(或任何更高版本)
我遇到了同样的问题。当我将 androidx.appcompat 从版本 1.2.0 升级到 1.3.1 时,问题就消失了。无需添加 androidx.activity 或 androidx.fragment 依赖项。
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
...
}
根据this documentation from Google about launching an activity to get a result:
While the underlying startActivityForResult() and onActivityResult() APIs are available on the Activity class on all API levels, it is strongly recommended to use the Activity Result APIs introduced in AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02.
我想让我的用户在我的应用程序中拍照,并将照片数据传回我的应用程序。我打算使用旧的 startActivityForResult() 但这个新方法看起来会解决很多问题并且更可靠,所以我想试一试。我应该能够调用 registerForActivityResult()
并传递给它一个内置的拍照合同 ActivityResultsContracts.TakePicture:
this.registerForActivityResult(new ActivityResultContracts.TakePicture(), ...);
但我得到:error: package ActivityResultContracts does not exist
我已将此添加到我的 app/build。gradle:
// original include
//implementation 'androidx.appcompat:appcompat:1.1.0'
// suggestion from Google documentation
//implementation 'androidx.appcompat:appcompat:1.2.0-alpha02'
// AndroidStudio suggested a newer version was available
implementation 'androidx.appcompat:appcompat:1.2.0-beta01'
我尝试了 alpha02
和 beta01
,但它们似乎都没有文档中提到的 classes。
当我尝试在 java 文件的顶部手动导入 class 时,AndroidStudio 认为该包也不存在。应该是androidx.activity.result.contract.ActivityResultContracts.TakePicture
,但这是我看到的:
我正在使用 gradle 3.5.3 如果这很重要的话。 (每当我尝试升级到最新的 gradle,我的项目就会变得疯狂,所以我一直使用有效的版本。)
来自引用的文档:
it is strongly recommended to use the Activity Result APIs introduced in AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02.
这些都不在您的依赖项中,至少是您问题中的部分。您一直在操纵 appcompat
,而不是 activity
或 fragment
。
添加其中一个或两个:
implementation "androidx.activity:activity:1.2.0"
implementation "androidx.fragment:fragment:1.3.0"
(或任何更高版本)
我遇到了同样的问题。当我将 androidx.appcompat 从版本 1.2.0 升级到 1.3.1 时,问题就消失了。无需添加 androidx.activity 或 androidx.fragment 依赖项。
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
...
}