英文:
How do I #include ActivityResultContracts
问题
当我尝试这样做:
import androidx.activity.result.contract.ActivityResultContracts
Android Studio 报错说“result”未定义。
我的 build.gradle 文件中有这行内容:
implementation 'androidx.appcompat:appcompat:1.2.0'
据我所知,这应该让我可以访问 ActivityResultContracts。我漏掉了什么?
英文:
When I try to do this:
import androidx.activity.result.contract.ActivityResultContracts
Android Studio complains that "result" is undefined.
My build.gradle has this line in it:
implementation 'androidx.appcompat:appcompat:1.2.0'
As far as I know, this should give me access to ActivityResultContracts. What am I missing?
答案1
得分: 1
根据从活动获取结果的文档,Activity Result API仅在Activity 1.2.0和Fragment 1.3.0中引入。AppCompat 1.2.0只在传递依赖于Fragment 1.1.0,而后者仅依赖于Activity 1.0.0。这意味着您没有足够新版本的Fragments和Activity来访问Activity Result API。
因此,您可以选择升级AppCompat版本(它确实依赖于更新版本的Fragment和Activity):
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
或者在Fragment上添加额外的依赖(这会引入正确版本的Activity):
implementation 'androidx.fragment:fragment-ktx:1.3.0-beta01'
英文:
As per the Getting a result from an activity documentation, the Activity Result API was only introduced in Activity 1.2.0 and Fragment 1.3.0. AppCompat 1.2.0 only transitively depends on Fragment 1.1.0, which in turn only depends on Activity 1.0.0. This means you don't have a new enough version of Fragments and Activity to have access to the Activity Result APIs.
Therefore you can either upgrade your version of AppCompat (which does depend on the newer version of Fragment and Activity):
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
Or add an additional dependency specifically on Fragment (which pulls in the correct version of Activity):
implementation 'androidx.fragment:fragment-ktx:1.3.0-beta01'
答案2
得分: 0
原来我误解了我得到的信息。build.gradle 中的实现行需要更改为以下内容。
implementation 'androidx.activity:activity-ktx:1.2.0-beta01'
英文:
Turns out I misinterpreted the information I was given. The implementation line in build.gradle needs to be this instead.
implementation 'androidx.activity:activity-ktx:1.2.0-beta01'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论