“cannot resolve symbol R” in Android Studio

2018-12-31 01:26发布

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.

30条回答
一个人的天荒地老
2楼-- · 2018-12-31 02:14

Have you updated your SDK tools recently? Launch the android SDK manager and 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 and reopened the SDK manager.

查看更多
浪荡孟婆
3楼-- · 2018-12-31 02:14

Do: Build > Clean Project

IMPORTANT: Make sure you don't have any Errors after Clean Project in Message Pane (Alt+0). If you find any red circles with exclamation mark, than you must remove those errors in your code.

Mostly these errors are related to @string/ or @array/. Clean Project again and done!

查看更多
长期被迫恋爱
4楼-- · 2018-12-31 02:15

I think if you put your Activities files to another folder than the default one. You need to import the com.example.yourproject.R (this is your project R file NOT Android.R file) to ALL activities using R. For example, in MainActivity file insert this line:

import com.example.yourproject.R;

查看更多
其实,你不懂
5楼-- · 2018-12-31 02:16

I had this this issue too. A simple 'gradlew clean' and 'gradlew build' did the trick.


Click on Build->Clean Project and that will perform a gradle clean

查看更多
一个人的天荒地老
6楼-- · 2018-12-31 02:22

`I had same problem and it solved by :

1) Sync Project with gradle files
2) Build -> Clean Project
3) Build -> Rebuild Project
4) File -> Invalidate caches

//imp step
5) Check your xml files properly.`
查看更多
看风景的人
7楼-- · 2018-12-31 02:23

This notation seems to work fine.

android:id="@+id/viewID"

Android Studio's design panel doesn't seem to work well.

查看更多
登录 后发表回答