gpt4 book ai didi

找不到 AndroidX ActivityResultContracts 包/找不到类

转载 作者:行者123 更新时间:2023-12-03 13:25:13 52 4
gpt4 key购买 nike

根据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'

我试过 alpha02beta01并且它们似乎都没有文档中提到的类。

当我尝试在我的 java 文件顶部手动导入该类时,AndroidStudio 也不认为该包存在。应该是 androidx.activity.result.contract.ActivityResultContracts.TakePicture ,但这就是我所看到的:

screenshot of Android Studio auto-complete package list in import statement

如果这很重要,我正在使用 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 ,而不是 activityfragment .
添加一个或两个:
implementation "androidx.activity:activity:1.2.0"
implementation "androidx.fragment:fragment:1.3.0"
(或任何更高版本)

关于找不到 AndroidX ActivityResultContracts 包/找不到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61622254/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com