LỖI R TRONG ANDROID STUDIO

Want lớn improve sầu this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
In every instance in all of my classes where I reference R.id.something, the R is in red and it says "cannot resolve sầu 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 lớn 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 sầu lớn be the most recent version of Android Studio & the project was created with Android Studio and worked without any "cannot resolve sầu R" problems. I would lượt thích to lớn know what causes this if anyone knows.

Bạn đang xem: Lỗi r trong android studio


game android android-studio r.java-file
Share
Follow
edited Nov 7 "18 at 8:36
*

Tyên
38.8k1717 gold badges117117 silver badges132132 bronze badges
asked Jun 11 "13 at 21:18
*

ez4nickez4nick
9,1541212 gold badges3434 silver badges5757 bronze badges
19
| Show 14
more comments

101 Answers 101


Active Oldest Votes
1
234 Next
728
I had this this issue too. A simple "gradlew clean" và "gradlew build" did the triông xã.

Cliông xã on Build->Clean Project và that will persize a gradle clean


Share
Follow
edited Feb 23 "15 at 14:29
*

tokhi
19k2222 gold badges8989 silver badges103103 bronze badges
answered Aug 12 "13 at 9:30
*

Vipassamãng cầu VijayaranganVipassamãng cầu Vijayarangan
8,61122 gold badges1414 silver badges20trăng tròn bronze badges
19
| Show 14 more comments
232
In the lachạy thử versions of Android Studio, at least for me, the following works:

"Tools" -> "Android" -> "Sync Project with Gradle Files"

In lakiểm tra Android Studio 3.1.3 (July 2018), "Sync Project with Gradle Files" is available in main thực đơn bar.

*


Share
Follow
edited Jul 31 "18 at 23:07
*

Om Sao
4,80211 gold badge2929 silver badges4848 bronze badges
answered Nov 19 "13 at 9:51
Tom C.Tom C.
2,35511 gold badge99 silver badges44 bronze badges
10
| Show 5 more comments
159
I was using gradle 3.3.0. It was the main culprit. Wasted 6.50 hours from my life. Gradle 3.2.1 removed the error.

classpath "com.android.tools.build:gradle:3.2.1"Problem resolved after changing the gradle version. Details history can be found here.


Share
Follow
edited Mar 22 "19 at 4:55
answered Jan 21 "19 at 9:05
Tushar MonirulTushar Monirul
4,43199 gold badges3333 silver badges4747 bronze badges
7
| Show 2 more comments
109
I have sầu a similar problem và here is what I did:

Clean Project và Sync Project with Gradle ,

*

check buildTools version in my sdk

*

From build gradle (module ) change minSdkVersion from 8 lớn 9

defaultConfig applicationId "PackageName" minSdkVersion 9 targetSdkVersion 22 versionCode 1 versionName "1.0" But all this didn"t help.

Finally I found the answer (this worked in my case)

Change from build.gradle(module:app)

game android compileSdkVersion 21 buildToolsVersion "21.0.1"..........Or

Select File | Project Structure change Build Tools Version to lớn 21.1.1

*


Share
Follow
edited Jan 17 "trăng tròn at 10:04
user12465043
answered May 3 "15 at 15:11
Mina FawzyMimãng cầu Fawzy
18.7k1515 gold badges121121 silver badges131131 bronze badges
0
Add a comment |
58
Cheông xã your xml files.Clean Project.This is it.

For example I had an entry in my strings.xml:

Some textAnd in activity_main.xml I used this string entry

While"d been working with project, I deleted that

Some textAnd consequently activity_main.xml became not valid. As the result: cannot resolve sầu R.

So, check your *.xml files và clean project.


Share
Follow
answered Mar 10 "15 at 22:12
Iurii VasylenkoIurii Vasylenko
2,8422121 silver badges2424 bronze badges
4
Add a phản hồi |
52
If you see this error after moving java files or directories to other locations, then you can guarantee that Android Studio has gotten confused. And guess what? undo-ing those actions doesn"t fix the problem.

So you try a clean, but that doesn"t work.

And restarting doesn"t work either.

But try File -> Invalidate Caches / Restart... -> Invalidate and Restart

Android Studio maintains information about which files are dependent on which other files. And since moving files around is not implemented correctly, moving files causes errors. And that"s not all: caches of these dependencies are used in an attempt to lớn tốc độ up the build.

This means you not only have khổng lồ restart, but you need khổng lồ invalidate those caches khổng lồ restore (or more accurately, rebuild) sanity.

Xem thêm: Các Game Thủy Thủ Mặt Trăng 24H, Cách Chơi Game Thủy Thủ Mặt Trăng Pha Lê


Share
Follow
edited May 25 "18 at 10:33
serv-inc
30.4k99 gold badges133133 silver badges152152 bronze badges
answered Oct trăng tròn "15 at 18:47
SMBiggsSMBiggs
9,36844 gold badges5656 silver badges7272 bronze badges
4
Add a phản hồi |
45
I think if you put your Activities files to another thư mục than the mặc định one. You need to import the com.example.yourproject.R (this is your project R tệp tin NOT Android.R file) khổng lồ ALL activities using R. For example, in MainActivity file insert this line:

import com.example.yourproject.R;
Share
Follow
edited May 3 "20 at 23:18
M.A.K. Ripon
1,62133 gold badges2525 silver badges4343 bronze badges
answered Mar 12 "14 at 7:40
ninjahoahongninjahoahong
2,4441919 silver badges2020 bronze badges
2
Add a comment |
27
There seems lớn be many causes for this issue. Recently, I added an activity to demo with called NewActivity. This created a tệp tin called res/menu/new.xml và gradle did not lượt thích that name because new is a reserved word.

gradlew clean

or

"Tools" -> "Android" -> "Sync Project with Gradle Files"

Those are definitely good things to lớn try, but you may have some other issues you need to resolve sầu before you run those commands.


Share
Follow
edited Aquảng bá 21 "16 at 15:32
Mr Robot
3,29177 gold badges3636 silver badges7070 bronze badges
answered Dec 3 "13 at 2:39
chawkinsufchawkinsuf
1,3011212 silver badges1313 bronze badges
4
Add a bình luận |
17
R.java file contains the liên kết between xml and java page. "R cannot be resolved" arise only if there is a problem with some of your resource files. So the best và effective sầu way is delete the last done xml or drawable in res tệp tin. & then again start from there according to lớn android coding norms. This is the only way. The effective sầu và right way.

Also use Git for proper tracking of code. ..


Share
Follow
edited Jun 22 "19 at 10:01
Zoe
24.1k1616 gold badges101101 silver badges142142 bronze badges
answered Apr 3 "14 at 3:43
MintoMinto lớn
1,91411 gold badge1313 silver badges1919 bronze badges
1
Add a bình luận |
14
Have you updated your SDK tools recently? Launch the apk SDK manager và make sure you have the latest SDK tools, which is now separate from the platform tools. I had this same issue when I first updated my SDK manager, the SDK build tools package did not show up for install/update until I closed và reopened the SDK manager.


Share
Follow
edited Jun 11 "13 at 22:59
answered Jun 11 "13 at 21:45
invertigoinvertigo
6,06044 gold badges3434 silver badges6060 bronze badges
2
Add a phản hồi |
14
Your code is just scrambled. The answer is fairly simple.

Just go to lớn Build --> Clean Project.

That should bởi the triông xã.

Check also your version of Android Studio.

I"m currently using Android Studio 3.0.1.


Share
Follow
edited Jan 17 "20 at 10:05
user12465043
answered Dec 21 "17 at 9:01
lloydyu24lloydyu24
56111 gold badge66 silver badges1313 bronze badges
Add a comment |
13
I had to lớn import my R package in game android studio. For ex: import com.example..R


Share
Follow
answered Jul 23 "14 at 21:11
Pervy SagePervy Sage
75311 gold badge88 silver badges2121 bronze badges
2
Add a comment |
11
Just clean your project and Sync Project with Gradle File.
*

And the problem will be resolved.


Share
Follow
answered Jan 16 "15 at 13:45
ArunendraArunendra
1,8262222 silver badges2121 bronze badges
1
Add a bình luận |
10
I have sầu had this with

An uppercase letter in my drawable resources.Import Android.R being added by Android Studio (or Eclipse) Error in xml tệp tin
Share
Follow
answered Sep 24 "13 at 14:54
Ryan HeitnerRyan Heitner
12k66 gold badges6666 silver badges107107 bronze badges
1
Add a bình luận |
10
I recently had this issue và the clean/build etc. didn"t resolve sầu it. I guessed I had an issue somewhere but wasn"t sure where to lớn look lớn find it (was it in the manifest, resource xml files etc?).

When something is wrong, you can find out what it is by doing a code analysis run.

From the menu at the top choose:

Analyze -> Inspect code, scan the whole project using the "Default" profile.

Your inspection results will contain an element called "Android". In here you will get a các mục of anything that is causing issues in your resources & manifest file. The errors will open up the generated xml files from your build which will show any errors in red.

My exact issue was caused because I was using flavour based manifest files and I copied some permissions inside the tag in the flavour by accident.

*

UPDATE: I did have sầu an issue after where Android Studio still showed the error after the fix, even though I could now build và run perfectly fine while the error was still there. (I am running a canary build so putting it down khổng lồ that for now)