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.


当前回答

考虑以下IDE功能:

用gradle文件同步项目 Build ->清洁项目 Build ->重建项目 File ->使缓存失效


我的简单解决方案是:

Check that there is no error line in the dependencies of the build.gradle of the app or in all the resource files in the /res directory; where some common errors are when the attribute values are left without references when using: "@string/.." "@style/.." Make sure you are not getting an error of the type Android resource linking failed when the project is rebuilt. Open the file that contains the line with the error Cannot resolve symbol 'R' Scrolling to the last line of the class with said error line Click on a position to the right of the closing key } of the class

通过这种方式,IDE警告对话框将执行r类的导入。

GL

其他回答

我也遇到过类似的问题,我是这样做的:

清洁项目和同步项目与Gradle,

检查buildTools版本在我的sdk

从build gradle (module)中将minSdkVersion从8修改为9

defaultConfig {
        applicationId "PackageName"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

但这一切都无济于事。

最后我找到了答案(这对我来说很管用)

从build.gradle(module:app)更改

android {
    compileSdkVersion 21
    buildToolsVersion '21.0.1'
......
....
}

Or

选择文件|项目结构将构建工具版本更改为21.1.1

我按照上面的方法做了,但不管用。在我的例子中,当我在Android-L预览中每个Java函数都显示为红色时,我不得不设置JDK……

在Mac:

Click once on what is Red (or underlined) in your code (i.e. Activity, String). Click on the Red Light Bulb Point that appears on the left. Select "Setup JDK". Verify if annotations.jar and android.jar are in the current SDK version you are using (my case SDK 20 Android-L). You should also have the "res" folder, but I somehow still had it in the settings. If not, select the SDK and press "-". Then press "+" and locate the SDK folder on your computer (Mac: Applications > Android Studio.app > sdk). Select the version of the SDK you require (in my case 20 Android-L). Press Add. Build and Sync.

然后一切都恢复了正常。

这个符号看起来很好用。

android:id="@+id/viewID"

Android Studio的设计面板似乎不太好用。

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

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

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

如果你在移动java文件或目录到其他位置后看到这个错误,那么你可以保证Android Studio已经混淆了。你猜怎么着?撤销这些操作并不能解决问题。

所以你试着戒毒,但没用。

重启也不行。

但是试试文件->无效缓存/重新启动…->失效并重启

Android Studio维护哪些文件依赖于哪些其他文件的信息。由于移动文件没有正确实现,因此移动文件会导致错误。这还不是全部:这些依赖项的缓存被用于加速构建。

这意味着您不仅必须重新启动,而且还需要使这些缓存失效以恢复(或者更准确地说,重新构建)正常运行。