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.


当前回答

还有一件事要尝试……

删除Build文件夹 通过同步gradle重新构建项目

我已经试过很多其他的建议了,比如

清洁项目, 重新启动Android Studio, 修复错误在我的xml, 同步项目与Gradle文件,以及 更新构建工具版本,

但这些都不起作用。

我最终手动删除了整个构建文件夹(在备份我的项目后),然后重新启动Android Studio。之后,我在我的应用程序构建中做了一个小编辑(添加和删除一个空格)。Gradle文件,然后点击同步。(我可能只需要从菜单中使用Gradle文件完成Tools > Android > Sync Project。)从那以后,它起作用了。在构建文件夹中已经损坏了一些东西,并开始与一个新的构建修复它。

我不知道有没有人读到这么远的答案,但我还是要加上这个答案。

其他回答

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

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

“我也遇到过同样的问题,解决方法是:

1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches

//imp step
5) Check your xml files properly.`

我也有同样的问题: Android Studio 3.2 该项目编译和运行良好,但我在屏幕上得到“无法解析符号“R”的警告

没有帮助:

构建——>清洁项目 构建- >重建项目 文件- >缓存失效/重新启动

帮助:

要么

在build.gradle

dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
...
}

替换为

   dependencies {
            classpath 'com.android.tools.build:gradle:3.2.1'
    ...
    }

or

更新Android Studio到3.3

在我的例子中,导入应用程序域名后跟“。R解决了这个问题。例子:

import com.example.myapp.R;

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

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