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问题部分的构建日志。
其他回答
The above answers are also the ways to fix the error but if does not work for anyone then i have an option...
只需要关闭项目,然后重新启动它,这样它就会加载引用… 跟着截图走。
我也有同样的问题。我尝试了上面提到的大多数其他人,即Gradle同步,验证缓存和重新启动,清洁和重建项目…重新启动android studio,仍然有同样的问题…
这是我的问题,我认为其他人可能也有同样的问题。我在新创建的布局文件的名称上有空格…而不是file_example.xml我是文件_example.xml **(注意文件和_之间的空格)**当你试图找到这个布局文件的引用时,你会遇到一个错误。
我建议您查看您刚刚创建的最新布局文件,稍微重构名称,然后将您的R.layout更改为activity/fragment中的新文件—> 然后清理..重建项目,项目将编译没有任何错误。至少对我来说,这是解决问题的办法。
这不是错误。 如果你有错误 关闭项目。 然后打开项目 在此之后,您可以得到一个错误
Cannot resolve symbol 'default_web_client_id'
可以通过在sting值中插入Web客户端ID来解决
考虑以下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
在我的例子中,当我试图从另一个包调用R时,出现了这个错误。导入R包解决了我的问题。
import com.mtdaps.yourapp.R;
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件