Android Studio 3.1.3 - 未解决的参考:R - Kotlin [英] Android Studio 3.1.3 - Unresolved reference: R - Kotlin

查看:46
本文介绍了Android Studio 3.1.3 - 未解决的参考:R - Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 kotlin 的新手,我已经从 Java 转换了一些代码,但似乎有问题,findViewById(R.id.my_id) 中的 R 以红色突出显示,并显示以下消息:未解析的引用:R..我一直在寻找解决方案,但我似乎没有弄清楚,那我该怎么办?截图如下:

I am new to kotlin, i have converted some code from java but it seems like there's something wrong, The R in findViewById(R.id.my_id) is highlighted in red and it shows this message : "Unresolved reference: R".. I've been looking for a solution but i seem not to figure it out, So what should i do? Here's a screenshot :

推荐答案

该问题可能由多种因素引起,

The issue can be caused by many factors,

  • 正如 martomstom 在此答案中提到的,该问题有时是由 com.android 引起的.tools.build:gradle 版本,将其版本更改为更稳定的版本即可解决问题:例如:com.android.tools.build:gradle:3.4.0-alpha02com.android.tools.build:gradle:3.2.1

  • as mentioned by martomstom in this Answer the issue is sometimes caused by com.android.tools.build:gradle version, changing it's version to a more stable one would solve the problem: for example: com.android.tools.build:gradle:3.4.0-alpha02 with com.android.tools.build:gradle:3.2.1

此外,具有来自同一组但具有不同版本的库可能会导致问题甚至更多运行时错误.使用排除组方法如下: implementation('com.squareup.picasso:picasso:2.71828') { exclude(group: 'com.android.support') } 在这种情况下,picasso 库使用 android.support 组件,picasso 中使用的 android 库版本与您当前在应用中使用的版本不同,因此为了解决此问题,我们必须将其从其子库和类组中完全排除.

Also, having libraries from the same group, but with different versions may cause the problem or even more runtime errors. use the exclude group method like the following : implementation('com.squareup.picasso:picasso:2.71828') { exclude(group: 'com.android.support') } in this case, picasso library uses android.support components, the android library version used in picasso is different than the one you're currently using in your app, so in order to solve this issue, we have to exclude it completely from its sub library and class groups.

这也可能是由于资源和代码不匹配而发生的,在您的活动中包含此导入行也可以解决问题:import com.package.name.R

It can also happen by the mismatch of resources and code, including this importation line in your activity may solve the problem too : import com.package.name.R

有时可能会因为 IDE、性能或内存而发生.不时清理项目可能会为您节省一些时间,在 Android Studio 上,它会是这样的:Build ->清理项目/重建项目 -清理IDE现金也有助于提高性能和内存,在Android Studio上它看起来像这样:File->使追逐无效/重新启动 ->使现金无效并重新启动

Sometimes it can happen because of the IDE, performances or memory.. Cleaning the project from time to time may save you some time, on Android Studio it would be something like this : Build -> Clean Project / Rebuild Project - Cleaning IDE cash also helps with performance and memory, on Android Studio it would look like this : File-> Invalidate Chases/ Restart -> Invalidate Cashes and Restart

我注意到这个问题在我导入新资源的大部分时间都会发生,在名称中使用禁止字符会触发错误,例如 ., , - , 大写或特殊字母

I noticed that this problem happens to me the most of the time when importing new resources, Using prohibited characters in their names would fire the error, such as . , , - , UpperCase or special Letters

作为一个建议,如果您使用的是 Kotlin,我真的建议您在您的活动中使用 Kotlin 扩展,例如:import kotlinx.android.synthetic.main.activity_page.* 或者如果您使用自定义视图:kotlinx.android.synthetic.main.view_layout.view.*之后,在活动的 onCreat() 方法中,您只需调用 id,例如:my_edit_text_ID.text = "Kotlin Dbest!",或从自定义视图:mCostumView.my_edit_text_ID.text = "Kotlin Dbest!"

And as a suggestion , if you're using Kotlin, i really recommend using Kotlin extensions in your activity such as : import kotlinx.android.synthetic.main.activity_page.* or if you're using a custom view : kotlinx.android.synthetic.main.view_layout.view.* after that, in onCreat() method of an activity , you'll only have to call the id, for example : my_edit_text_ID.text = "Kotlin Dbest!", or from a custom view : mCostumView.my_edit_text_ID.text = "Kotlin Dbest!"

  • 我再次遇到了这个问题,问题是R"库是从 2 个不同的来源导入的:

  • I have faced this issue againe and the problem was the '' R '' library was imported from 2 different sources :

com.android.R

com.example.package.R

您必须只使用您的应用程序包名称导入 '' R '' 库,在这种情况下 com.example.package.R有时库根本没有导入,要导入它,点击未解析的引用 R 并按 Alt + Enter

You must only import the '' R '' library with your application package name, in this case com.example.package.R Sometimes the library is not imported at all, to import it, click on the unresolved reference R and press Alt + Enter

正如评论部分中提到的tobltobs:大多数情况下,问题是由另一个错误引起的,该错误阻止构建系统创建生成的源.要找到根本原因,请查看 gradle 日志(构建输出中绿色锤子下方的切换视图"图标)并查找与 R 或 BuildConfig(也已生成)无关的错误.如果没有其他错误并且 R 的问题仍然存在,那么此列表中的某些内容可能会有所帮助."

As tobltobs mentioned in the comments section: " Most of the time the problem is caused by another error which prevents the build system from creating generated sources. To find the root cause look at the gradle log (the "toggle view" icon below of the green hammer in the Build output) and look for errors unrelated to R or BuildConfig (also generated). If there is no other error left and the problem with R persists then maybe something of this list might help. "

正如 Patrick Beagan 提到的,Kotlin 扩展现在已被弃用 - 我建议改用 ViewBinding

As Patrick Beagan mentioned, Kotlin extensions are now deprecated - I'd advise using ViewBinding instead

这篇关于Android Studio 3.1.3 - 未解决的参考:R - Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆