1

Just started Android Programming but as soon as I create a new projects 2 errors come up both saying:

R cannot be resolved as a variable

I searched the error online and found TONS of questions with this topic, however no solutions work for me.

P.S I am using the latest version of Eclipse

Edit I just created the project and this error comes right away.

enter image description here

Here is the code:

package com.example.firstapp;

import android.os.Bundle; import android.app.Activity; import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); //error
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu); //error
    return true;
}

}
15
  • Specify what's the error, when does it occurs and maybe post some code Commented Aug 12, 2013 at 1:27
  • The error is above and I just created the project. I do not have any code. Commented Aug 12, 2013 at 1:28
  • Please share the Screen shot of errors along with your question Commented Aug 12, 2013 at 1:40
  • Are you using more than one packages in your src folder ? Commented Aug 12, 2013 at 1:44
  • Take a look at stackoverflow.com/questions/7824730/… Commented Aug 12, 2013 at 1:45

10 Answers 10

2

To fix by re-installing android SDK is a way but what about a quick-win by adding import android.R; good luck next time, It did not fix for me yet

Sign up to request clarification or add additional context in comments.

Comments

1

Do you have errors in any of your resource files? The 'R' file may not be created if there are errors in the resources. For example, check if any of your layout files has errors.

2 Comments

@AbdullahShafique One error which won't show up as an error is having any invalid characters in your XML file names, such as capital letters.
@netinept The only capital letter xml is "AndroidManifest.xml"
1

Try creating a new workspace. For some reason the same thing happened to me until I created a new workspace in a DIFFERENT location. Just a thought. The problem definitely sucks.

Comments

1

I have normally seen this problem when something in the Android sdk is broken.

Are you using eclipse with ADT or a stand-alone download?

If it is eclipse alone, do you have the android sdk installed and in your build path?

Comments

1

I think you are facing Build Errors Just go into your project properties -> Builders and Uncheck Java Builder.
Then rebuild, this should solve the problem.

EDIT:
Check all your XML files and verify that the first line should be this:
<?xml version="1.0" encoding="utf-8"?>
If you don't see this first line in any XML file, just paste this on the first line and clean & rebuild the project. If the error still remains after editing the XML files then just restart Eclipse.

Also if there are any import android.R; statements in your code are present, then just remove them.

1 Comment

try unchecking all of them one by one and rebuilding the project
1

Double check you haven't imported android.R.

It sneaks in from time-to-time as you add android resources. Eclipse thinks its doing you a favor, so that you don't have to fully specify Android.R.resources, but the import prevents you from seeing your local r (i.e. whats in your project folders) which messes up your project.

1 Comment

I checked no imported android.R.
1

Previously this happened to me. Believe it or not, what I tried was creating another new project with whatever you tried in previous project. I've been facing many problem with this Eclipse, and many of them can be simply solve by restarting it or create another new project by replicating it

Comments

1

Thanks for everyone who helped me! I just had to re-install the android SDK!

Comments

1

What have you tried from the other answers? When I have this problem I:

  1. Fix any issues with the XML files, they all must compile with no errors in order for R to be generated. If that doesn't work:
  2. Clean project
  3. Restart Eclipse
  4. Check if "Build Automatically" is selected.

4 Comments

I tried everything you said and "Build Everything" is selected. The error is still there.
Did you try starting a new project? Are there any errors when you start Eclipse or start a new project?
Whenever I start a new project the errors come. I created at least 6 projects and they all have the error.
Running Eclipse as Admin might be worth a shot. If not, maybe try to reinstall SDK and Eclipse?
0

For future visitors - Hello future visitors! - try checking your Android Manifest for errors, such as the wrong package name. When you refactor a project, it will change all the references to the package name except in the manifest.

You should also look at your layout xml files for errors. If all else fails start over.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.