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 version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/add_user"
        android:icon="@android:drawable/ic_menu_add"
        android:title="Add User"
        app:showAsActioin="always"
        />
</menu>

Android工作室没有检查应用程序名称空间的属性,似乎文件是无错误的。现在,在构建项目时,它说无法解析符号“R”。错误显示在构建日志中。

修复错字修复它,手表上的Android问题部分的构建日志。

这个符号看起来很好用。

android:id="@+id/viewID"

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

我有我的XML文件在一个布局文件夹,而不是菜单文件夹。

这是我们从Eclipse转移到Android Studio的结果。

解决方案对我来说很简单……将XML文件移动到MENU文件夹并重新编译。

只需清理你的项目和同步项目与Gradle文件。

问题会得到解决。

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