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插件从1.3.1版本升级到

classpath group: 'com.android.tools.build', name: 'gradle', version: '2.2.1'

它迫使我改变了所有的引用,比如

import [applicationId].R;

to

import [package].R;

在我的库项目中,appcontext.getPackageName();停止正常工作。我用它来获得正确的应用程序资源通过反射,但升级gradle插件后,它开始返回特定口味的applicationId,而不是包…你可以在这里阅读更多相关内容。

其他回答

在新的android studio 2.2中。如果你的TargetSDK =< 15,那么你也可能会发现这个问题。在gradle中改变这个。构建,然后按照前面的答案执行步骤。同步gradle文件并重建。您还可以使缓存无效并重新启动工作室,这有时也有帮助。

我受够了

我可绘制资源中的一个大写字母。 导入安卓。R由Android Studio(或Eclipse)添加 xml文件错误

确保项目中有AndroidManifest.xml。

我导入的项目还没有manifest,所以Android Studio不会将其识别为Android项目。然后我在GIT中拉了这个项目,它下载了清单,但仍然不能工作。 然后我重新启动Android Studio,它说“框架检测到Android”。

确保你的项目配置为Android项目。

我想我有终极解决方案了。我做了一项研究,发现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同步项目失败”等。

这个符号看起来很好用。

android:id="@+id/viewID"

Android Studio的设计面板似乎不太好用。