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.
当前回答
我按照上面的方法做了,但不管用。在我的例子中,当我在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.
然后一切都恢复了正常。
其他回答
我正在做一个多模块的项目。这发生在我把所有ui代码移到一个新模块时。
下面是我解决这个问题的方法: 我发现当我创建新的ui模块时,在/res/values中,它创建了一个新的strings.xml,其中没有任何有用的东西。 我删除了strings.xml文件,重新构建项目,它工作了。
伙计们,我觉得这里有很多是瞎拍。在某些情况下,只有在解决了问题之后,“清洁和同步项目”才会有所帮助。
步骤1: 因此,去看看每个文件的顶部,其中包被指定如下:package ="YourURL.YourProject";并确保指定了正确的包(Your Own Project)。 你会在java文件和AndroidManifest.xml中发现这一点,对于java文件来说,引用正确的包是至关重要的,因为这个包包含了你所指向的资源(“R”)。如果他们不匹配,错误不能解决符号R发生。
步骤2: 清洁,同步,等等。完成
为什么这是随机发生的,或者我做错了什么?
如果复制和粘贴代码,应该密切注意上面解释的“package=”。更重要的是,当你粘贴代码时,它会立即通过某种调试器(请原谅我的糟糕技术术语)向你显示“假定错误”,它会立即考虑“错误的包”,你会得到所有的错误。 因此,即使您在粘贴后立即更正了符号,调试器也已经刷新。这就是为什么“清洁,同步,随便什么”有时管用
我已经做了其他答案中提到的所有步骤,但它仍然没有得到解决。我正在使用android工作室花栗鼠,最后我能够通过删除来解决这个问题
maven { url "https://jitpack.io" }
从设置。gradle我在回答这个问题,我认为这样做可能会帮助到一些人
你们最近更新了SDK工具吗?启动android SDK管理器,确保你有最新的SDK工具,现在是独立于平台工具。当我第一次更新我的SDK管理器时,我也遇到了同样的问题,SDK构建工具包在安装/更新时没有出现,直到我关闭并重新打开SDK管理器。
首先检查是否有任何错误在任何xml布局,如果然后解决它首先。
否则,从项目中删除junit依赖项并重新构建项目。
推荐文章
- 我可以设置“android:layout_below”在运行时编程?
- Android Studio谷歌JAR文件导致GC开销限制超过错误
- 为什么每当我创建一个新项目时,Eclipse都会自动添加appcompat v7库支持?
- 如何同步项目到GitHub与Android工作室?
- Keytool错误:java.io。IoException:错误的AVA格式
- GCM与PHP(谷歌云消息传递)
- 自定义圆圈按钮
- 我如何使用一个复合绘图而不是一个线性布局,包含一个ImageView和一个TextView
- 如何绘制一个线性布局的边界
- 如何动画添加或删除Android列表视图行
- 如何解决“丢失的PendingIntent可变性标志”在android api 30+ lint警告?
- 我怎么能修复“意外元素<查询>发现<manifest>”错误?
- Android房间-自动生成新插入行的id
- Android Studio在哪里保存ProGuard映射文件?
- 我如何从资产文件夹解析一个本地JSON文件到一个ListView?