Java 无法在 Android 中解析 ContextCompat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31733044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:31:57  来源:igfitidea点击:

Cannot Resolve ContextCompat in Android

javaandroid

提问by Mark F

I have an AlertDialogue object called dialog. I am attempting to add an icon to it. I see that this syntax is now deprecated:

我有一个名为 dialog 的 AlertDialogue 对象。我正在尝试为其添加一个图标。我看到现在不推荐使用此语法:

dialog.setIcon(getResources().getDrawable(R.drawable.myImage);

I'm reading everywhere that this should work:

我到处都在阅读这应该有效:

dialog.setIcon(ContextCompat.getDrawable(context, R.drawable.myImage));

However, the ContextCompat syntax is not being recognized by Android Studio. Is there something that I should be importing? Thank you.

但是,Android Studio 无法识别 ContextCompat 语法。有什么我应该导入的吗?谢谢你。

***Update: Thank's to @Sharj for the correct answer below. I made a quick video too if you guys need a visual: https://www.youtube.com/watch?v=eFiaO0srQro&feature=youtu.be

***更新:感谢@Sharj 在下面提供正确答案。如果你们需要视觉效果,我也制作了一个快速视频:https: //www.youtube.com/watch?v=eFiaO0srQro&feature=youtu.be

采纳答案by Sharj

ContextCompatis part of support library v4. Have you added support library 4 to your project?

ContextCompat是支持库 v4 的一部分。您是否在项目中添加了支持库 4?

android.support.v4.content.ContextCompat

You can include support library to your build.gradle file under app folder if you haven't already

如果您还没有,您可以在 app 文件夹下的 build.gradle 文件中包含支持库

dependencies {
// other stuff here
    compile 'com.android.support:support-v4:23.0.0'
// update the 23.0.0 to latest version available

}

回答by Akinola Olayinka

If you are using Android gradle plugin 3.0.1, add google() to your allProjects repositories in the build.gradle file (project level) then sync

如果您使用的是 Android gradle 插件 3.0.1,请将 google() 添加到 build.gradle 文件(项目级别)中的 allProjects 存储库,然后同步

like this:

像这样:

allprojects {
    repositories {
        google()
        ....
        //other repos
    }
}

回答by Svetoslav Marinov

I had the same issue and this and a few more posts helped me. With Android studio you have multiple Gradle files.

我有同样的问题,这个和更多的帖子帮助了我。使用 Android Studio,您可以拥有多个 Gradle 文件。

I got my code to work by adding the dependencies section into Gradle (Module : Library) or the file that has "android {" ...

我通过将依赖项部分添加到 Gradle(模块:库)或具有“android {”...

dependencies {
        // other stuff here
        compile 'com.android.support:support-v4:23.+'
        // update the 23.0.0 to latest version available
    }

回答by trustidkid

Adding this to build.gradle(Module:App) under dependencies resolved the problem

将此添加到依赖项下的 build.gradle(Module:App) 解决了问题

compile 'com.android.support:support-v4:23.0.0'

回答by Atetc

androidx.core.content.ContextCompat

from AndroidX dependency

来自 AndroidX 依赖

implementation 'androidx.appcompat:appcompat:1.1.0'