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.
当前回答
在我的情况下,我试图清理和重建项目。这个问题没有修复,因为在布局文件中存在错误。为了重新创建,我在应用程序名称空间的属性上添加了一个错字。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add_user"
android:icon="@android:drawable/ic_menu_add"
android:title="Add User"
app:showAsActioin="always"
/>
</menu>
Android工作室没有检查应用程序名称空间的属性,似乎文件是无错误的。现在,在构建项目时,它说无法解析符号“R”。错误显示在构建日志中。
修复错字修复它,手表上的Android问题部分的构建日志。
其他回答
//有时由于不完全同步会产生问题
//所以只要按照步骤,它会做你的工作肯定
Click on Build->Clean Project and that will perform a 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来做到这一点。
我受够了
我可绘制资源中的一个大写字母。 导入安卓。R由Android Studio(或Eclipse)添加 xml文件错误
我也遇到过同样的问题,大多数时候都是通过
同步项目与gradle文件 做构建->清洁项目 Doing File ->使缓存失效
但这一次,即使在做了所有这些事情后,错误仍然存在,最后我找到了罪魁祸首。
问题是在一个xml文件,在那里我给了一个普通的字符串值为android:id而不是id资源类型。
错误
android:id="username"
修正
android:id="@id/username"
混淆xml文件中与资源相关的内容是一个主要问题 此错误的原因。注意,它可能不会在xml布局文件中显示为错误。
请注意
在大多数情况下,编译器会在消息中显示错误的来源。 在尝试其他解决方案之前,请先检查它
只需进入Android Top菜单列表。点击构建菜单,在构建下点击重建项目。
推荐文章
- 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
- 按钮背景是透明的