未找到 AndroidX ActivityResultContracts 包/未找到类 [英] AndroidX ActivityResultContracts package not found / class not found

查看:116
本文介绍了未找到 AndroidX ActivityResultContracts 包/未找到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据

如果这很重要的话,我正在使用 gradle 3.5.3.(每当我尝试升级到最新的 gradle 时,我的项目都会变得疯狂,所以我一直使用有效的版本.)

解决方案

来自引用的文档:

<块引用>

强烈建议使用 AndroidX Activity 1.2.0-alpha02 和 Fragment 1.3.0-alpha02 中引入的 Activity Result API.

这些都不在您的依赖项中,至少是您问题中的部分.您一直在操纵 appcompat,而不是 activityfragment.

添加其中一个或两个:

实现androidx.activity:activity:1.2.0";实现androidx.fragment:fragment:1.3.0";

(或任何更高版本)

According to 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.

I want to let my users take a photo from within my app and get that photo data back to my app. I was going to use the old startActivityForResult() but this new method looks like it will solve lots of problems and be more reliable, so I wanted to give it a try. I'm supposed to be able to call registerForActivityResult() and pass it a built-in contract for taking a picture called ActivityResultsContracts.TakePicture:

this.registerForActivityResult(new ActivityResultContracts.TakePicture(), ...);

But I get: error: package ActivityResultContracts does not exist

I've added this to my 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'

I tried the alpha02 and the beta01 and neither of them seem to have the classes referred to in the documentation.

When I try to import the class manually at the top of my java file, AndroidStudio doesn't think the package exists either. It should be androidx.activity.result.contract.ActivityResultContracts.TakePicture, but this is what I see:

I'm using gradle 3.5.3 if that matters at all. (Whenever I try to upgrade to the latest gradle, my project goes insane, so I've just been staying with the version that works.)

解决方案

From the quoted documentation:

it is strongly recommended to use the Activity Result APIs introduced in AndroidX Activity 1.2.0-alpha02 and Fragment 1.3.0-alpha02.

Neither of those are in your dependencies, at least the portion from your question. You have been manipulating appcompat, not activity or fragment.

Add either or both of:

implementation "androidx.activity:activity:1.2.0"
implementation "androidx.fragment:fragment:1.3.0"

(or any higher versions)

这篇关于未找到 AndroidX ActivityResultContracts 包/未找到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆