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依赖,

compile 'com.android.support:appcompat-v7:22+'

我的compilesdk版本是 编译esdkversion 21,所以android工作室不高兴。我简单地更新了我的compilesdkversion到23,并进行了同步。

我还将依赖项更新为

compile 'com.android.support:appcompat-v7:23.1.1'

匹配我的支持库在工具—> Android—> SDK管理器—> SDK工具选项卡—> Android支持库—>版本。 现在一切都好了。快乐编码:)

其他回答

你应该做两件事,首先清理项目(在build菜单中)-它会删除build目录,这可能是罪魁祸首:

接下来,同步项目与Gradle文件(在文件下):

这是在Windows 10上的Android Studio 3.6.1中项目的位置。

如果所有这些都失败了,无效缓存和重新启动(在文件下)通常可以做到这一点。在我看来,这将关闭整个程序并占用大量时间。

澄清一下,我是在windows 10上运行这个程序,但它应该也能在MacOS和Linux上运行。

我在androidmanifest中有android:textAllCaps = "false",这对R文件造成了严重破坏。我删除了,一切正常。我认为目标sdk设置得不够高。

检查xml文件。 清洁项目。

就是它了。

例如,我在strings.xml中有一个条目:

<string name="A">Some text</string>

在activity_main.xml中,我使用了这个字符串条目

<TextView
android:id="@+id/textViewA"
android:text="@string/A"/>

当我一直在做这个项目的时候,我把它删除了

 <string name="A">Some text</string>

因此activity_main.xml就无效了。结果:不能解析R。

因此,检查您的*.xml文件并清除项目。

你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。

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

然后一切都恢复了正常。