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.


当前回答

确保项目中有AndroidManifest.xml。

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

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

其他回答

建立>清洁项目

这对我很管用。同样的问题我也遇到过几次,这似乎解决了问题。除非你改变了一些东西或变量r,这个问题通常是突然发生的,当它发生在我身上时,所以我想这只是Android工作室吓坏了。哈哈

祝你过得愉快,并祝你的项目好运。

您只需要检查Xml文件。 如果出现错误,则无法解决此错误。 只需解决xml错误,然后尝试。

OR

清理你的项目从构建->清洁

这个符号看起来很好用。

android:id="@+id/viewID"

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

我现在也有类似的问题。问题是在我的项目中,我有更多的模块。例如app模块和库模块。虽然类和R.string资源都来自app模块,但我在那里导入了com.package.library.R。不知怎的,Android Studio没有意识到这是一个错误,Gradle/Sync构建我的项目没有问题。但是当我试图运行单元测试时,Gradle构建失败,出现了所描述的错误。

package com.package.app

import com.package.library.R;
//...

class SomeClass {
  //...
  public void someMethod() {
     Toast.make(context, R.string.message, TOAST_LONG).show();
  }
}

当r .string. message资源实际上在我的com.packagename.app模块而不是com.packagename.library模块时。删除导入…解决了我的问题。

如果你在.xml文件上有任何错误,无论你做什么,你都无法修复这个问题。我建议

-首先,检查XML文件以查找任何错误和

-秒查找事件日志什么Android工作室捕捉到的错误,它会告诉你哪个文件和错误在哪里。

这就是我解决R问题的方法