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

查看:237
本文介绍了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
  • 同样,具有来自同一组的库,但具有不同的版本,则可能会导致此问题,甚至可能导致更多的运行时错误.请使用如下所示的排除组方法:implementation('com.squareup.picasso:picasso:2.71828') { exclude(group: 'com.android.support') }在这种情况下,毕加索库使用android.support组件,毕加索中使用的android库版本与您当前在应用程序中使用的android库版本不同,因此为了解决这个问题,我们必须将其完全排除在其子库和类组之外.

  • 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
  • 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

  • 我又遇到了这个问题,问题是"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.android.R

com.example.package.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. "

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

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