Error: R cannot be resolved

I am creating an Android project and I am facing the following error in my IDE.

package br.com.app;
import android.R;

public class MinhaApp extends Activity {

     @Override
     public void onCreate (Bundle savedInstanceState) {
         super.onCreate (savedInstanceState);
         setContentView (R.layout.main);
     }
}

But my IDE gives the following error:

R cannot be resolved

In line

setContentView (R.layout.main);

Why is this happening? The file maix.xml is in the folder res/layout correctly.

Author: Maniero, 2013-12-11

9 answers

Give Clean and then Build in your project.

 31
Author: iTSangar, 2013-12-11 17:01:19

You imported the 'wrong' R by mistake. Change the line of import to:

import <pacote_do_projeto>.R

For example:

import br.com.app.R

If the project package is 'br. com. app'

 10
Author: Alexandre Atoji, 2013-12-11 20:56:54

This often happens when you have just created a project, which seems to me to be your case, so you can see from the code.
So just relax and wait a little.
It turns out that as soon as you create a new project, several processes are running at the same time (the Java compiler, the Android Development Tool generators, the other IDE plugins, etc.)...). So sometimes it takes a little while for ADT to generate the R Class.
If this does not happen, just follow the answer of @iTSangar.

 5
Author: Ecil, 2013-12-11 20:19:50

This is a common error that can come from several different origins:

  • error in one of the XMLs (even if it does not Charge);
  • Do not save before Clean / Build;
  • some error that is preventing the automatic generation of this file;

I discovered a solution so far "definitive" here pro personnel where I work.

EDIT.: This form should be used as the last case, if none of the above alternatives worked:

Since naturally Eclipse generates the file and something was blocking this procedure, I chose to do it manually. I went to the directory (Agenda / gen / br/com/contacts / UI) (for example), inside the project folder, I created a copy of BuildConfig and edited its name to R. Then I created a new project and copied the code from R to the copy. Finally, in the contact calendar project, I gave Save, Clean and Build all so that the settings were updated and the errors (generated by the lack of the same) correct. In this way, the new R class is adapted according to the requests of the created code.

I did not test in every possible way, however, it served everyone I indicated. I do not know to what extent it is correct to carry out the procedures in this way, so I ask you to give me feedback if it does not work out.

 5
Author: Leonardo Saldanha, 2014-03-14 12:32:30

This can occur for several reasons, but the most common are:

  1. newly created Project. Give Clean and Build.
  2. Class Order in Java Build Path is incorrect or wrong priority. Give preference to the following: Android Dependencies, Android Private Libraries, the other
  3. error in some xml (layout, drawable, etc.).
 4
Author: Leonardo Cardoso, 2014-01-31 18:38:49

What is r

When developing Android applications, the implemented features are defined by an external XML file. This guarantees the division between the presentation layer and the application layer, in the background the separation of the logic of the appearance.

An xml file is created that stores all the information contained in the 'res' folder of the project.

The eclipse ADT plugin accesses all resource references through the R class, which is governed whenever a new resource is defined in the project or whenever the project is rebuilt.

 2
Author: Tiago Antunes, 2014-01-29 14:51:03

Whenever this happens clean resolves. But you've heard many times that it didn't even work out that way. Delete class R.java, I had to close the eclipse and give the clean, as the eclipse recreates.

 1
Author: Nellonidas, 2013-12-12 15:48:30

Import is wrong. If there is any error in the xmls, the R class is not generated. Read the warnings and you'll know what's wrong with your project.

 1
Author: Jodson Leandro, 2014-01-29 16:18:25

Sometimes just close the project and open again resolves. Otherwise delete, close and reopen also resolves.

 -3
Author: Laercio Bernardo, 2014-02-01 14:54:38