19

Using Eclipse on win XP. I've previously created an app or 2, but am now working on the 'real' version and get the above error.

I was having funnies creating a 1.5 version, so have started a new project for 2.1.

package xxx.guest;

import android.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class briefme extends Activity {

 private String strURL;
           ........
           ........ 

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

Project as below (note I've removed some names for confidentiality reasons!)

Any ideas what's gone wrong?

Thanks,

Martin.

Project screenshot here

NPike
  • 13,136
  • 12
  • 63
  • 80
Martin Moore
  • 733
  • 2
  • 8
  • 21

8 Answers8

59

I would try refreshing. If that doesn't work, close and open the project.

If those two fail, delete and re-create R (it should auto-generate -- copy it just to be on the safe side).

I've had this happen to me a few times and those are the three things I usually do to fix the error.

Also try changing your import:

import android.R;

to:

import your.package.R;
Null
  • 1,950
  • 9
  • 30
  • 33
James
  • 5,622
  • 9
  • 34
  • 42
  • 2
    > my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem? > Look at your imports. Chances are that the line: import android.R; Taken from another Stackoverflow Q : will be there. If that's the case, remove it, so that your project will resolve R not with the default Android Resources class, but with the one auto-generated from your /res/ folder. – Martin Moore Oct 27 '10 at 16:16
  • 6
    Worked when changed to `my.package.R` – Janis Peisenieks Mar 19 '12 at 07:14
  • 1
    Regenerating R can be found here: http://stackoverflow.com/questions/2757107/developing-for-android-in-eclipse-r-java-not-generating – Clinton Apr 09 '12 at 04:42
  • Thanks, this worked in NetBeans (7.2) too. In addition after adding that extra import I can also remove it the id remains visible. – Akos K Aug 26 '12 at 17:17
  • +1 `import you.package.R;` solved my problem as well. Thank you so very much. – methodMan Nov 13 '12 at 20:26
  • In my case I had to **delete** import android.R and went back to normal. – Kovács Imre Nov 15 '13 at 16:32
3

In my case this happened because their was a layout resource with upper case letters in the file name. Resources weren't compiled because of the error and because of that the R class was missing.

2

As suggested above, simply changing the import "import android.R;" to one relative to your app e.g. "import your.package.name.R;" should do the trick here.

1

You can also check if adb program in /platform-tools folder has permissions to run in your system. If not, the R.java for your project won't be generated.

0

My two cents.

In my case, i had used another variable with the same name R which resulted in compilation error. Eclipse could get better in terms of guiding the users with the exact error message.

this-Me
  • 2,139
  • 6
  • 43
  • 70
0

Possible duplicate of R cannot be resolved - Android error.

In that thread we have an excellent answer.

Referring to http://source.android.com/source/using-eclipse.html we get the following snippet:

Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.

So the answer is to delete rather than rename your import android.R;

The reason for this is that your.package.R is visible since you share package. The import android.R; masks this, so it is cleaner to delete this rather than changing it to something redundant.

Community
  • 1
  • 1
RandomSort
  • 600
  • 7
  • 22
0

If you have 64 bit Linux platform without ia32 libraries, then this problem could also occur. It took me quite some time to pin this one down, seeing that the problem is not clearly linked with the inability to execute 32 bit code.

NTwoO
  • 75
  • 6
0

In my case (seeing other tutorials) you have to change:

setContentView(R.layout.main ); 

To:

setContentView(R.layout.activity_main);
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Chuck
  • 1