Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android studio suddenly cannot resolve symbol R

I was having a project open in Android studio. It was generated by the wizard and working fine.

I did some small changes to activity_main.xml and when I changed back to MainActivity.java i get the error in several places that it cannot resolve R. I might have done something to cause this. But I´m not sure what since it appered when I edited the xml.

Does anyone know what might be the solution to this? I can find the R.java in r/release/packege/com/ and it looks fine.

like image 217
Einar Sundgren Avatar asked Jun 14 '13 15:06

Einar Sundgren


People also ask

What is the R in Android Studio?

R is created as part of building your project. To allow code-completion and other assistance features of Android Studio to work, R gets re-created whenever you add, rename, or remove a resource, as R is an “index” of all of the resources in your app.

What does Cannot resolve symbol mean?

The cannot find symbol error, also found under the names of symbol not found and cannot resolve symbol , is a Java compile-time error which emerges whenever there is an identifier in the source code which the compiler is unable to work out what it refers to.

Where is the R Java file in Android Studio?

R. java is the generated file by ADT or Android studio. It will be located under app\build\generated\source\r directory.

What is Cannot resolve method in Java?

In JavaLanguage, if you get 'cannot resolve symbol', you have used a name that the compiler hasn't recognized. Class names -- If it is a class name, the compiler cannot find the class.


2 Answers

I just had this problem and the reason was that after editing the layout android studio automatically added an import to the source files:

import android.R

Of course the main R class has not all the symbols in your layouts, which made everything a red mess. Removing that line solved for me.

EDIT: Seems like eclipse had the same problem early on.

like image 76
Roberto Piva Avatar answered Sep 16 '22 14:09

Roberto Piva


In addition, make sure you do not have a drawable with an invalid name. I had a drawable with a numeric filename and that didn't sit well with Android so it failed to compile R.java.

like image 42
Tash Pemhiwa Avatar answered Sep 19 '22 14:09

Tash Pemhiwa