eclipse – R.layout.main cannot be resolved-ThrowExceptions

Exception or error:

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

How to solve:

I would try refreshing, if that doesnt 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 have this happen to me a few times and those are the three things I usually do to fix the error.

Edit: Also try changing your import:

import android.R;

to:

import your.package.R;

###

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.

###

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.

###

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.

###

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.

###

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.

###

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.

###

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

setContentView(R.layout.main ); 

To:

setContentView(R.layout.activity_main);

Leave a Reply

Your email address will not be published. Required fields are marked *