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.


当前回答

这是一个非常古老的问题,但它仍然经常发生,这里没有真正全面的答案。

我遇到这个问题的次数比我想承认的要多。它可能是由各种各样的问题引起的,大多数问题都与您的项目结构不符合预期有关。下面是我遇到的所有问题的清单:

从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来做到这一点。

其他回答

我受够了

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

我也有同样的问题。我尝试了上面提到的大多数其他人,即Gradle同步,验证缓存和重新启动,清洁和重建项目…重新启动android studio,仍然有同样的问题…

这是我的问题,我认为其他人可能也有同样的问题。我在新创建的布局文件的名称上有空格…而不是file_example.xml我是文件_example.xml **(注意文件和_之间的空格)**当你试图找到这个布局文件的引用时,你会遇到一个错误。

我建议您查看您刚刚创建的最新布局文件,稍微重构名称,然后将您的R.layout更改为activity/fragment中的新文件—> 然后清理..重建项目,项目将编译没有任何错误。至少对我来说,这是解决问题的办法。

在最新版本的Android Studio中,至少对我来说,如下作品:

“工具”->“Android”->“同步项目与Gradle文件”

在最新的Android Studio 3.1.3(2018年7月)中,“同步项目与Gradle文件”在主菜单栏中可用。

最近,当我更新一个项目的很多库时,我就遇到了这种情况。没有找到R,但是我可以很好地构建和运行项目。它没有崩溃。IDE只是在所有地方显示红色。

我试过

Check for damaged/improper XMLs and images, none found Check for invalid resource file names (e.g. capitalized), none found Downgrade some libraries because maybe it was a resource conflict, not resolved Lowered compileSdkVersion and targetSdkVersion because, in the past, I encountered resource issues due to this. In this project however, that was not the case. Clean, didn't help Rebuild project, didn't help Invalidate caches/Restart multiple times, didn't help Adjusted idea.max.intellisense.filesize to a very high value, didn't help Did a Lint run, no errors found

最后,我发现降级我的Gradle构建工具(从3.3.1到3.2.1)解决了这个问题。这是我唯一要做的改变。其余内容已更新。现在,我所有的库都更新了,没有问题。

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        //classpath 'com.android.tools.build:gradle:3.3.1'
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.google.gms:google-services:4.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

对我来说,重要的是说Android Studio在一开始就会给学习带来麻烦,但就像这里有解决方案清洁项目可以解决这个问题,总是检查包装的名称,不要试图混乱的逻辑,我喜欢软件,但有时我试着思考如果方式是正确的或不正确,我也喜欢Linux,但对我来说,Android Studio重写所有软件也许这不会发生。总之,祝你好运!!!!