“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:09

Just go to Android Top menu list. click on Build Menu, in under Build click on Rebuild Project.

enter image description here

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

I had the same problem, and it happens when I create a new project.

What I do is:

  • check for SDK updates
  • then android studio updates,
  • then reopen the project
  • open the andoridmanifest.xml
  • erase a space between a "_>" in the android:label and save.

That works for me.

查看更多
荒废的爱情
4楼-- · 2018-12-31 02:10

Just clean your project and Sync Project with Gradle File.enter image description here

And the problem will be resolved.

查看更多
姐姐魅力值爆表
5楼-- · 2018-12-31 02:12
  1. Check your xml files.
  2. Clean Project.

This is it.

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

<string name="A">Some text</string>

And in activity_main.xml I used this string entry

<TextView
android:id="@+id/textViewA"
android:text="@string/A"/>

While'd been working with project, I deleted that

 <string name="A">Some text</string>

And consequently activity_main.xml became not valid. As the result: cannot resolve R.

So, check your *.xml files and clean project.

查看更多
浅入江南
6楼-- · 2018-12-31 02:13

I had a hard time fixing this myself.

  • Make sure you have no errors in your layout.xml files.
  • Go to Build > Clean project

It worked for me, hope it works for you too.

查看更多
流年柔荑漫光年
7楼-- · 2018-12-31 02:13

change the latest sdk version,that was work for me

1.Click file and then click project structure 2.then select "app" then click ok,its worked for me. enter image description here

查看更多
登录 后发表回答