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.


当前回答

在我的例子中,当我试图从另一个包调用R时,出现了这个错误。导入R包解决了我的问题。

import com.mtdaps.yourapp.R;

其他回答

看看Gradle控制台。在我的例子中,我包含了一个文件扩展名错误的资源。

AAPT: libpng error: Not a PNG file
:app:mergeDebugResources FAILED

FAILURE: Build failed with an exception.

在重命名文件和同步项目与Gradle文件之后,一切都很好。

我今天升级了gradle版本和gradle插件版本的最新版本,它为我工作。

之后,同步项目与gradle文件。

//有时由于不完全同步会产生问题

//所以只要按照步骤,它会做你的工作肯定

Click on Build->Clean Project and that will perform a gradle clean

其中一个原因可能是,你最近修改了布局/资源文件,资源(字符串/颜色/可绘制等)在你的代码中不可用,但你在设计中使用了参考。

试着寻找这样的引用,删除/注释它们,并尝试重新构建。

我也有同样的问题: Android Studio 3.2 该项目编译和运行良好,但我在屏幕上得到“无法解析符号“R”的警告

没有帮助:

构建——>清洁项目 构建- >重建项目 文件- >缓存失效/重新启动

帮助:

要么

在build.gradle

dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
...
}

替换为

   dependencies {
            classpath 'com.android.tools.build:gradle:3.2.1'
    ...
    }

or

更新Android Studio到3.3