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.


当前回答

显然,在我的例子中,这个问题通过在结尾添加一个“*”得到了解决

import android.R.*;

其他回答

当我导入google服务时,我就遇到了这个问题。json文件来实现分析。我已经在xml文件夹中有global_tracker.xml文件。在构建过程中,合并来自google-services的内容。Json文件,错误开始发生。

删除google -services后,该错误得到解决。json文件。

这些在Ubuntu 12.04上都不适用。我必须做到以下几点:

sudo apt-get install lib32stdc++6
sudo apt-get install lib32z1

在那之后,我遵循了这个线程中的其他一些建议,做了Build ->清洁项目,一切都很好。

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

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

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

只需进入Android Top菜单列表。点击构建菜单,在构建下点击重建项目。

我现在也有类似的问题。问题是在我的项目中,我有更多的模块。例如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模块时。删除导入…解决了我的问题。