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.


当前回答

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

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

其他回答

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

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

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

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

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

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

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

伙计们,我觉得这里有很多是瞎拍。在某些情况下,只有在解决了问题之后,“清洁和同步项目”才会有所帮助。

步骤1: 因此,去看看每个文件的顶部,其中包被指定如下:package ="YourURL.YourProject";并确保指定了正确的包(Your Own Project)。 你会在java文件和AndroidManifest.xml中发现这一点,对于java文件来说,引用正确的包是至关重要的,因为这个包包含了你所指向的资源(“R”)。如果他们不匹配,错误不能解决符号R发生。

步骤2: 清洁,同步,等等。完成

为什么这是随机发生的,或者我做错了什么?

如果复制和粘贴代码,应该密切注意上面解释的“package=”。更重要的是,当你粘贴代码时,它会立即通过某种调试器(请原谅我的糟糕技术术语)向你显示“假定错误”,它会立即考虑“错误的包”,你会得到所有的错误。 因此,即使您在粘贴后立即更正了符号,调试器也已经刷新。这就是为什么“清洁,同步,随便什么”有时管用

我按照上面的方法做了,但不管用。在我的例子中,当我在Android-L预览中每个Java函数都显示为红色时,我不得不设置JDK……

在Mac:

Click once on what is Red (or underlined) in your code (i.e. Activity, String). Click on the Red Light Bulb Point that appears on the left. Select "Setup JDK". Verify if annotations.jar and android.jar are in the current SDK version you are using (my case SDK 20 Android-L). You should also have the "res" folder, but I somehow still had it in the settings. If not, select the SDK and press "-". Then press "+" and locate the SDK folder on your computer (Mac: Applications > Android Studio.app > sdk). Select the version of the SDK you require (in my case 20 Android-L). Press Add. Build and Sync.

然后一切都恢复了正常。

在我做“建立->清洁项目”和“同步项目与gradle”。它们都不是解。我从3.3.0 => 3.2.1(恢复为项目初始状态)下构建gradle版本,它解决了我的问题。

请看下面的图片去文件->项目设置->设置所有设置像图像和同步它将把所有最新的设置在你的项目。 如果您的项目导入成功,那么您必须这样做。因为它会导入所有的更新设置和gradle。