Android Studio 3.1.3 - Unresolved reference: R - K

2020-06-30 09:09发布

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 :

enter image description here

15条回答
我命由我不由天
2楼-- · 2020-06-30 09:30

This worked for me. How much work it is depends on how big your project is. I started a new project, created the required modules (XML, Kotlin, colors, strings, etc.), then copied the code into the modules in the new project from the modules in the old project. Copying XML saves a lot of time compared to recreating the UI. All in all, it take a little while, but I have spent much more time tring to fix the unresolved reference error without it.

查看更多
地球回转人心会变
3楼-- · 2020-06-30 09:31

I used com.android.tools.build:gradle:3.3.0-alpha13 and had the same issue. Changing to stable Version 3.2.1 solved this problem for me.

查看更多
淡お忘
4楼-- · 2020-06-30 09:34

Here is the solution, File->Project Structure->Project, select Android Gradle Plugin Version as 3.2.1 from the drop-down. then click apply.

查看更多
劳资没心,怎么记你
5楼-- · 2020-06-30 09:35

TRY THIS

Go to the content_main.xml file and there you need to change the

android:id="@+id/??????"> line of code to whatever id you have given to your file.

Replace question mark ?????? with the related file id name.(IF you dont know the id go to the design tab on the bottom and click on the related Asset.

On the right side below attributes, you can find the ID you have given to it. If it is blank you can freshly name it and Android Studio will write the code.

Then restart Android Studio. Hope this will help. Happy coding.

查看更多
祖国的老花朵
6楼-- · 2020-06-30 09:35

Downgrading gradle version worked for me.

I changed :

Gradle version from : 4.10.4 to 4.4.1

and Gradle Plugin version from : 3.3.1 to 3.1.3

查看更多
The star\"
7楼-- · 2020-06-30 09:41

I had the same problem with R reference too. Finally Android Studio 3.3 has been released and using 'com.android.tools.build:gradle:3.3.0' the problem has been fixed!

查看更多
登录 后发表回答