简体   繁体   English

R.layout.main 无法找到/无法解析符号 R

[英]R.layout.main Cannot Be Found / Cannot Resolve Symbol R

All of a sudden I am getting red underlinings under my references to elements eg R.textboxID.突然之间,我在引用元素(例如 R.textboxID)时出现了红色下划线。 I did some googling around and I found that the error occurs when Android Studio can't generate your R.java file correctly.我在谷歌上搜索了一下,发现错误发生在 Android Studio 无法正确生成您的 R.java 文件时。

My question is how does Android Studio just suddenly not know how to generate my R file correctly when it was generating it well all along?我的问题是 Android Studio 一直在很好地生成我的 R 文件,但它怎么突然不知道如何正确生成它? How can I fix this problem permanently?我怎样才能永久解决这个问题?

This usually happens when some unexpected event, like an IDE error occurs and the project needs to be rebuilt.这通常发生在某些意外事件(例如 IDE 错误)并且需要重建项目时。 Just hit Build -> Rebuild Project.只需点击 Build -> Rebuild Project。 Sometines this may occur after you modify your build.gradle file, and sometimes, if it only occurs within one of your classes, you might not be inflating it correctly.在某些情况下,这可能会在您修改build.gradle文件后发生,有时,如果它只发生在您的一个类中,您可能没有正确地对其进行膨胀。

在 Android Studio 中重建项目

Select Build > Clean Project from the Android Studio toolbar, wait a few moments, and then build your project by selecting Build > Rebuild Project.从 Android Studio 工具栏中选择 Build > Clean Project,稍等片刻,然后通过选择 Build > Rebuild Project 来构建您的项目。

If a single clean/rebuild cycle doesn't work, then try repeating this process a few times, multiple clean/rebuild cycles in quick succession usually work.如果单个清理/重建周期不起作用,则尝试重复此过程几次,快速连续的多个清理/重建周期通常会起作用。

Gradle sync succeded? Gradle 同步成功了吗? Try one of the following:尝试以下方法之一:

  • Clean end rebuild your project干净的结束重建你的项目
  • From Android studio menu: File->Invalidate cache/restart, then clean and rebuild project从 Android studio 菜单:File->Invalidate cache/restart,然后清理并重建项目
  • If none of above helps, close Android studio and remove ~/.gradle directory.如果以上都没有帮助,请关闭 Android Studio 并删除~/.gradle目录。 Then run Android Studio, open your project, wait for finish sync, then rebuild project然后运行Android Studio,打开你的项目,等待完成同步,然后重建项目

Also make sure your gradle configuration is correct and resource xml files syntax is correct.还要确保您的 gradle 配置正确且资源 xml 文件语法正确。

Please check the import statements at the top of file some times there may be import com.android.R will be there so you should delete that statement and it will work fine请检查文件顶部的导入语句,有时可能会有 import com.android.R,所以您应该删除该语句,它会正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM