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.


当前回答

注意:这可能只在您的项目非常大时才有用

TL;DR -生成的R文件超过2.56 MB, Android Studio没有索引它,你可以在这里遵循解决方案:

文件大小超过配置限制(2560000),代码洞察功能不可用

也面临着类似的问题。但这里的答案似乎都不奏效。

R文件大于2.56 MB,在打开生成的R文件时,我收到一条消息说“代码洞察可能不可用”

改变默认的想法。属性,以允许更大的文件,这是有效的。

很抱歉在移动设备上设置了格式。

其他回答

考虑以下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

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

清洁项目和同步项目与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

这个符号看起来很好用。

android:id="@+id/viewID"

Android Studio的设计面板似乎不太好用。

做:建立>清洁项目

重要:确保在消息窗格(Alt+0)中清除项目后没有任何错误。如果您发现任何带有感叹号的红色圆圈,那么您必须删除代码中的这些错误。

这些错误大多与@string/或@array/有关。再次清洁项目,完成!

我有这个问题,特别是当我不小心损坏布局或菜单资源文件。我碰到的无法解析符号R的情况是,当我忘记添加“@+id”时,我必须在菜单项中定义一个id。

使用损坏的菜单资源文件,如果我试图用它构建项目,就会得到这个错误。因为R是一个资源的容器类,当gradle不能解析资源的.xml文件时,它似乎不能绑定R。

解决方案是简单地修复资源文件,清理项目并重新构建。