Reputation: 6918
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.
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;
}
}
Upvotes: 1
Views: 1197
Reputation: 1
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.
Upvotes: 0
Reputation: 21
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
Upvotes: 2
Reputation: 9784
What have you tried from the other answers? When I have this problem I:
Upvotes: 1
Reputation: 6918
Thanks for everyone who helped me! I just had to re-install the android SDK!
Upvotes: 1
Reputation: 5550
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
Upvotes: 1
Reputation: 222
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.
Upvotes: 1
Reputation: 6714
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.
Upvotes: 1
Reputation: 161
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?
Upvotes: 1
Reputation: 782
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.
Upvotes: 1
Reputation: 294
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.
Upvotes: 1