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.


当前回答

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

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

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

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

其他回答

我知道这个问题可能会导致很多事情。在我的例子中,我在其中一个活动*.xml文件中有一些textView。我在编辑器中放入文本(稍后通过代码更改):“NAME: <这里将是名称>”。显然,“<”和“>”字符破坏了在我的项目中从每个地方导入R。我把它换了,它开始工作了。我只是忘记了不能在*.xml文件中使用这些字符,因为它们构建了它的结构。记住这一点是值得的。

我受够了

我可绘制资源中的一个大写字母。 导入安卓。R由Android Studio(或Eclipse)添加 xml文件错误

确保项目中有AndroidManifest.xml。

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

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

在我的情况下,代码在我更新android studio之前正常工作。更新后未生成R文件。我正在处理一个自定义视图,并且有一个属性

<attr name="showText" format="boolean" />

在我的declare stylable中。同样的属性也被系统的ui使用:

<style name="Base.Widget.AppCompat.CompoundButton.Switch" parent="android:Widget.CompoundButton">
    <item name="track">@drawable/abc_switch_track_mtrl_alpha</item>
    <item name="android:thumb">@drawable/abc_switch_thumb_material</item>
    <item name="switchTextAppearance">@style/TextAppearance.AppCompat.Widget.Switch</item>
    <item name="android:background">?attr/selectableItemBackgroundBorderless</item>
    <item name="showText">false</item>// this line
</style>

我将之前的showText重命名为showTextFormat,然后gradle sync重新生成R.java文件。

以上答案都不能解决我的问题。所以我添加了我的解决方案,以防有人遇到类似的问题。在我的构建中。我有gradle依赖,

compile 'com.android.support:appcompat-v7:22+'

我的compilesdk版本是 编译esdkversion 21,所以android工作室不高兴。我简单地更新了我的compilesdkversion到23,并进行了同步。

我还将依赖项更新为

compile 'com.android.support:appcompat-v7:23.1.1'

匹配我的支持库在工具—> Android—> SDK管理器—> SDK工具选项卡—> Android支持库—>版本。 现在一切都好了。快乐编码:)