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.


当前回答

最近,当我更新一个项目的很多库时,我就遇到了这种情况。没有找到R,但是我可以很好地构建和运行项目。它没有崩溃。IDE只是在所有地方显示红色。

我试过

Check for damaged/improper XMLs and images, none found Check for invalid resource file names (e.g. capitalized), none found Downgrade some libraries because maybe it was a resource conflict, not resolved Lowered compileSdkVersion and targetSdkVersion because, in the past, I encountered resource issues due to this. In this project however, that was not the case. Clean, didn't help Rebuild project, didn't help Invalidate caches/Restart multiple times, didn't help Adjusted idea.max.intellisense.filesize to a very high value, didn't help Did a Lint run, no errors found

最后,我发现降级我的Gradle构建工具(从3.3.1到3.2.1)解决了这个问题。这是我唯一要做的改变。其余内容已更新。现在,我所有的库都更新了,没有问题。

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        //classpath 'com.android.tools.build:gradle:3.3.1'
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.google.gms:google-services:4.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

其他回答

我有这个问题,特别是当我不小心损坏布局或菜单资源文件。我碰到的无法解析符号R的情况是,当我忘记添加“@+id”时,我必须在菜单项中定义一个id。

使用损坏的菜单资源文件,如果我试图用它构建项目,就会得到这个错误。因为R是一个资源的容器类,当gradle不能解析资源的.xml文件时,它似乎不能绑定R。

解决方案是简单地修复资源文件,清理项目并重新构建。

在我的情况下,我试图清理和重建项目。这个问题没有修复,因为在布局文件中存在错误。为了重新创建,我在应用程序名称空间的属性上添加了一个错字。

<?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问题部分的构建日志。

我自己也费了好大劲才搞定的。

确保layout.xml文件中没有错误。 转到构建>清洁项目

这招对我管用,希望对你也管用。

在我的案例中,通过将Android Studio从3.1更新到3.3解决了这个问题

要更新您的Android Studio:文件->设置->系统设置->更新,然后点击立即检查。

我在清理建筑。Gradle和这个错误出现。

一大堆“清理和重建”的答案。这些对我都没用。

最后,我发现我无意中复制粘贴了build.gradle中错误的“命名空间”名称。修复了它,它运行。