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-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.

然后一切都恢复了正常。

其他回答

Build ->清理项目或重建项目 或 File -> Invalidate cache / Restart…->失效并重启

我正在做一个多模块的项目。这发生在我把所有ui代码移到一个新模块时。

下面是我解决这个问题的方法: 我发现当我创建新的ui模块时,在/res/values中,它创建了一个新的strings.xml,其中没有任何有用的东西。 我删除了strings.xml文件,重新构建项目,它工作了。

清洁,重建和“缓存删除”不会帮助我,更新Android工作室修复了这个问题。

     The above answers are also the ways to fix the error but if does not work for anyone  then i have an option...

只需要关闭项目,然后重新启动它,这样它就会加载引用… 跟着截图走。

尝试确保完整的包名在第一行。假设您有一个名为LoginActivity.java的文件和一个名为com.domainname.project的包。

java的第一行应该是

package com.domainname.project;

这相当于在dot.net中指定“namespace”。还要确保该包与包含*.java文件的包匹配。