2

I have tried finding the answer to this question on here before, but none of the fixes have worked for me. Basically, my Android project is refusing to run - even though I haven't edited it at all from the default program that is given when you make a new project.

I have a red line under R on Eclipse, throwing up the error "R cannot be resolved to a variable". It suggests that I import android.R, but when I do this, another red line comes up, this time under activity_main, saying it "cannot be resolved or is not a field".

I have absolutely no idea what is going on here, and am not prolific in coding or programming at all, so any answers in absolute layman's terms would be really great. My code so far is below:

package com.randomproject.thebasics;

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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
CuriousCabbage
  • 414
  • 1
  • 5
  • 10
  • You don't need to import android.R, but you do need to make sure your resource file is generated by Eclipse. You should have a file res/layout/activity_main.xml which is used to create gen/com.randomproject.thebasics/R.java – Adrian G Feb 28 '13 at 19:56
  • There might be a problem in your manifest file. Check it. – Ariel Magbanua Feb 28 '13 at 20:02

2 Answers2

1

Try doing a Project->Clean, and then a full re-build (Ctrl+B). Eclipse sometimes goes a bit wonky with auto-generated files.

Al.
  • 2,285
  • 2
  • 22
  • 30
1

As noted in the comments this question has been answered thousands of times. Well, ok, only tens of times. There are two possiblities:

1) Resource error There is an error in your manifest or one of the files in your res directory. The resource compiler cannot run and there is no R.java class in the gen directory. Look in the gen folder, if there's no R.java file there, this is your problem.

2) Bad import Sometimes in an attempt to help you, eclipse will add the import android.R to your imports and then collapse the import section so you can't see it. None of your references to your R.whatever resources will work after that. Check the imports. Delete android.R if it is there.

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40