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.
当前回答
我也有这个问题。简单的“gradlew clean”和“gradlew build”就成功了。
点击Build->Clean Project,将执行gradle Clean
其他回答
这是一个非常古老的问题,但它仍然经常发生,这里没有真正全面的答案。
我遇到这个问题的次数比我想承认的要多。它可能是由各种各样的问题引起的,大多数问题都与您的项目结构不符合预期有关。下面是我遇到的所有问题的清单:
从Eclipse导入到Android Studio并不总是很顺利,尤其是旧版本的Android Studio。确保您使用了正确的目录树。对于一个适度成熟的应用程序,它应该是这样的:
AppName/ // AppName is your app's name, obviously
.gradle/ // This is a compiler created directory. You should normally leave it alone
.idea/ // Ditto
build/ // This too
gradle/ // And this
app/
build/
libs/
src/
androidTest/ // Tests based on the Android instrumentation runner should go here
main/
java/
fully/ // Typically com
qualified/ // the domain name. Longer package
// names may have deeper folder trees
AppName/ // All your java files go here
res/
drawable-*/ // One of these for each resolution you support
layout/ // All of your general layouts
menu/ // All of your menu layouts
values/ // All of your resource xml files
xml/ // PreferenceScreen layouts go here
AndroidManifest.xml
debug/
test/ // Pure jUnit tests should go here
.gitignore
app.iml
build.gradle // This is the gradle file you should be making most changes to
proguard-rules.pro
.gitignore
build.gradle // This gradle file should mostly be left alone
gradle.properties
gradlew
local.properties
AppName.iml
settings.gradle
在您的IDE中可能不是这样。在文件树上面有一个下拉菜单,可以以不同的方式显示你的文件,在我的系统上,它默认是一个叫做Android的系统,它把你的文件分成不同的类型。从Project中更容易修复这类问题,甚至直接从操作系统的文件系统中更容易修复,因为Android Studio将只包含另一个文件夹的文件夹折叠到一行中。
特别是如果你改变了你的应用程序的名称,你需要确保源树main/java/com/domain/AppName被更新。还要确保AndroidManifest.xml中的包标签是正确的。
If there are errors in either your Gradle files or your AndroidManifest.xml, this will prevent Android Studio from properly building your resource files. Gradle files can be broken by upgrading Android Studio sometimes, especially from the pre-1.0 versions. Sometimes this is because it stops supporting older versions of the Gradle plugin, so you need to update your version numbers. It can sometimes be hard to find what the current versions are. As of today, 7/17/15, my apps are compiling fine with com.android.tools.build:gradle:1.2.3. This is in the dependencies block in the outermost gradle file,
如果你的AndroidManifest引用了一个不存在的可绘制或字符串资源或活动,它会中断并导致这个错误。有时,如果任何东西引用了不存在的可绘制资源或字符串资源,就会得到这个错误。
如果您的资源中有一个损坏的文件,或者一个无效的xml文件,您将得到这个错误。
根据我的经验,有时候Android Studio会莫名其妙地出现问题,你需要重新启动它和/或你的电脑。我不知道为什么,但有时确实有效。
如果您有两个具有相同名称的xml资源,并且位于不相互覆盖的目录中,您可能会遇到这个问题。例如,你可以在drawable-mhdpi和drawable-xhdpi中使用相同的名称,因为它们会根据目标设备的不同而相互覆盖,但是如果你在布局和菜单中使用相同的名称,就会产生问题。重命名或删除其中一个文件。
如果只有一些资源存在此问题,那么这些资源很可能在错误的目录中。
有一次我不得不完全重新安装Android Studio。我不知道出了什么问题,但它起作用了。
在一种情况下,我将整个项目移动到另一个目录,并将其作为一个新项目重新导入。我不知道出了什么问题,但它起作用了。
使用保留字作为名称的Xml文件可能会导致此问题。重命名或删除它们。
您的Gradle文件有几种方法可以最终引用您没有安装的构建工具版本。通过更改Gradle或下载适当的构建工具来纠正这一点。
最后,在你修复了任何错误之后,你需要清理你的Gradle项目。您可以通过转到顶部的Build菜单并选择Clean Project来做到这一点。
我有我的XML文件在一个布局文件夹,而不是菜单文件夹。
这是我们从Eclipse转移到Android Studio的结果。
解决方案对我来说很简单……将XML文件移动到MENU文件夹并重新编译。
对于这个问题,我有一个特例。
在我的项目中,一切都很顺利,它可以编译和构建成功,但在我的Android Studio IDE(我也尝试了Intelligent IDEA,它们是在相同的情况下),R.java文件不能很好地解析,总是掉在红线中。
就像这样:
这几乎让我疯了,我不能忍受读取颜色当我编程。
最后我发现这个棘手的问题是由我的R.java的文件大小引起的。 我的项目非常庞大,它支持多种语言和多种屏幕尺寸。资源太多了,我的R.java文件大小大约是2.5M。
Android Studio中文件的最大大小默认设置为2.5M,因此大于这个限制的文件不能很好地解析。您可以在“AndroidStudio-root/bin/idea.properties”中更改默认设置。
更改这一行:
idea.max.intellisense.filesize=2500
to :
idea.max.intellisense.filesize=5000
然后重新启动Android studio,红色消失,我又可以愉快地编程了。
类型1:关闭项目并删除项目中的.gradle文件 然后重建项目 类型2:如果类型1不起作用,那就把它拿回来 project9Zip文件),然后删除项目。然后提取 压缩项目,只是导入提取的项目,它将工作 很好,因为这对我很管用。
在一个非基础特性的即时应用活动中,你可以调用R.id.fromFeatureOne和R.is.fromFeatureTwo。因此,来自不同资源的项目被引用为单一资源来源。
两者都将被高亮显示为可接受的,您甚至可以跳转到源代码。但是构建会导致一个错误:“不能找到符号R.id”,这是相当棘手的原因,因为什么都没有突出显示。
解决方案是调用com.example.feature.one.R.is.fromFeatureOne和com.example.feature.two.R.is.fromFeatureTwo。
在谷歌:https://issuetracker.google.com/u/0/issues/77537714也发布了一个bug
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的