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 Studio讨厌在安装路径或目录中设置“空白”。它不接受放置在包含空白的目录中的SDK。例如

C: \ Program Files \ Android \ sdk (x86)

此目录无效,因为它包含空白。在这里,我已经回答了一个问题,一个人无法在Android Studio中更改他的JDK路径,因为他的SDK目录中有一个空白。这很奇怪,但似乎Android Studio讨厌有空格的目录。所以解是-

将Android Studio和Android SDK安装在一个没有任何空白的目录或路径中。

例如-安装Android Studio

C:\AndroidStudio\

并安装Android SDK

C:\Sdk\

这两个目录都是有效的,因为没有任何空白。每个不包含空格的路径都是有效的。它还解决了其他问题,如“Failed to resolve: junit:junit:4.12”或“Gradle同步项目失败”等。

其他回答

大多数情况下,此问题是由XML文件引起的,请查看XML文件,查找任何错误。

这个问题似乎有很多原因。最近,我添加了一个名为NewActivity的活动来测试。这创建了一个名为res/menu/new.xml的文件,gradle不喜欢这个名字,因为new是一个保留字。

gradlew戒毒

or

“工具”->“Android”->“同步项目与Gradle文件”

这些绝对是值得尝试的好方法,但是在运行这些命令之前,您可能需要解决一些其他问题。

我也遇到过同样的问题,当我创建一个新项目时,这种情况就会发生。

我所做的是:

检查SDK更新 然后是安卓工作室更新, 然后重新打开项目 打开andoridmanifest.xml 在android:标签中删除“_>”之间的空格并保存。

这对我很有用。

我确实做了一个项目,而且成功了 但在此之前,请尝试检查清单中的包名称,重新构建并清理项目。如果问题没有解决,那就放弃你的项目

我按照上面的方法做了,但不管用。在我的例子中,当我在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.

然后一切都恢复了正常。