7

Just after creating the new Android Application Project I get the error "R cannot be resolved to a variable" in my MainActivity.java file.

This is it's content:

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

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

}

Why am I getting this problem if the app was created by Eclipse choosing all the default options? How can I fix this issue?

Karthik T
  • 31,456
  • 5
  • 68
  • 87
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

2 Answers2

6

It might be because, there might be some problem in your res folder,

  • The Main reason might be there could be wrong import file of R.java. So Remove, if any import android.R;

  • There might be some images with same name, or some files with capital letters, or even there might be some errors in your xml files,

  • Try rebuilding and cleaning your project.

For some quick solutions to this problem, have a look at this,

R.java file not getting created

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
-1

first try to clean your project and after that build it through eclipse.If the problem persists after this operation than it means its because of your activity_main.xml

Vsw10
  • 143
  • 3
  • 10