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.

然后一切都恢复了正常。

其他回答

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

gradlew戒毒

or

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

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

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

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

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

在我的例子中,当我试图从另一个包调用R时,出现了这个错误。导入R包解决了我的问题。

import com.mtdaps.yourapp.R;

在我的情况下,原因是有一些图像在res文件夹名称包含一个大写字母..

只要用小写字母重新命名就可以了