Error: R can not be resolved

16

I'm creating an Android project and am experiencing 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 can not be resolved

on the line

setContentView (R.layout.main);

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

    
asked by anonymous 11.12.2013 / 17:51

9 answers

30

Give Clean and then Build in your project.

    
11.12.2013 / 18:01
9

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

import <pacote_do_projeto>.R

For example:

import br.com.app.R

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

    
11.12.2013 / 21:56
4

This often happens when you just create a project, which seems to be your case, so you can see the code. So just relax and wait a little bit. 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, ...). So sometimes it takes a little bit for ADT to generate class R.
If this does not happen, just follow the response from @iTSangar.

    
11.12.2013 / 21:19
4

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

  • Error in one of the XMLs (even if it does not acknowledge);
  • Do not save before Clean / Build;
  • Some errors that are preventing the automatic generation of this file;

I have found a solution that has been "definitive" here for the people I work for.

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

Since of course 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, created a copy of BuildConfig and edited its name for R. Then I created a new project and copied the R code for copy. Lastly, in the contact agenda project, I gave Save, Clean and Build All so that the settings were updated and the errors (generated by the lack thereof) corrected. In this way, the new R class is adapted according to the requests of the created code.

I did not test in all possible ways, but it did for everyone I indicated. I do not know to what extent it is correct to do the procedures in this way, so please give me feedback if it does not work.

    
14.03.2014 / 12:58
3

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

  • Newly created project. Give Clean and Build.
  • Class order in Java Build Path is incorrect or wrong priority. Leave the following preference: Android Dependencies, Android Private Libraries, the others
  • Error in some xml (layout, drawable, etc.).
  • 31.01.2014 / 19:38
    1

    Whenever this happens, clean resolves. But he has heard times that he has not resolved. Deleting the class R.java, I had to close the eclipse and give the clean, as the eclipse recreates.

        
    12.12.2013 / 16:48
    1

    What is R

    When developing android applications, deployed features are defined by an external XML file. This is ensured the division between presentation layer and the application layer, in the background separation of logic from appearance.

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

    The Eclipse ADT plugin accesses all resource references through the class R, which class is regenerated whenever a new resource is defined in the project or whenever the project rebuilds.

        
    29.01.2014 / 15:51
    1

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

        
    29.01.2014 / 17:18
    -3

    Sometimes just closing the project and opening it again resolves. Otherwise deleting, closing and reopening also resolves.

        
    01.02.2014 / 15:37