In every instance in all of my classes where I reference R.id.something, the R is in red and it says "cannot resolve symbol R". Also every time there is R.layout.something it is underlined in red and says "cannot resolve method setContentView(?)". The project always builds fine. It is annoying to see this all the time. I have read many other questions on here about something similar but most involved importing projects from Eclipse. I am using what I believe to be the most recent version of Android Studio and the project was created with Android Studio and worked without any "cannot resolve R" problems. I would like to know what causes this if anyone knows.


当前回答

只需进入Android Top菜单列表。点击构建菜单,在构建下点击重建项目。

其他回答

考虑以下IDE功能:

用gradle文件同步项目 Build ->清洁项目 Build ->重建项目 File ->使缓存失效


我的简单解决方案是:

Check that there is no error line in the dependencies of the build.gradle of the app or in all the resource files in the /res directory; where some common errors are when the attribute values are left without references when using: "@string/.." "@style/.." Make sure you are not getting an error of the type Android resource linking failed when the project is rebuilt. Open the file that contains the line with the error Cannot resolve symbol 'R' Scrolling to the last line of the class with said error line Click on a position to the right of the closing key } of the class

通过这种方式,IDE警告对话框将执行r类的导入。

GL

在我的情况下,我需要修复我的布局XML文件中的一些错误。没有指示文件包含错误的图标。然而,当我点击它们时,我注意到一些红线。修好后,找到了R。

我也遇到过类似的问题,我是这样做的:

清洁项目和同步项目与Gradle,

检查buildTools版本在我的sdk

从build gradle (module)中将minSdkVersion从8修改为9

defaultConfig {
        applicationId "PackageName"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

但这一切都无济于事。

最后我找到了答案(这对我来说很管用)

从build.gradle(module:app)更改

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.1'
......
....
}

Or

选择文件|项目结构将构建工具版本更改为21.1.1

我有未解决的R问题,因为相同的image.png文件在两个地方。一个在res>anim和相同的文件在res>drawable-hdpi。这导致R无法解决。 问题是我制作了一个启动画面图像,并反复将其从Moray图形目录直接移动到Android项目中。我一定是不小心把它扔进了res>anim文件夹。我通过从res>anim文件夹中删除重复的图像文件来解决这个问题,Android工作室纠正了自己。

你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。