Saad Rehman
Saad Rehman

Reputation: 403

R cannot be resolved to a variable eclipse android

I already tried all the answers present in this website. Deleted the "import android.R;" line. Even then every time I run my project it says "Your project contains errors, fix them before running your application". I'm working on Eclipse. As soon as I create a new android application project I am encountered with 2 errors which say “R cannot be resolved to a variable”. Here's my code for MainActivity.Java

    package com.example.myfirstapp;


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


    class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentViewq(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.main, menu);
        return true;
    }

}

Upvotes: 1

Views: 29295

Answers (15)

Mehdi
Mehdi

Reputation: 934

if this error occur immediately after create your project on eclipse. the main reason is manifest.xml error. application icon path is wrong. copy ic_launcher.png into drawable folders.

Upvotes: 0

Arundas K V
Arundas K V

Reputation: 809

I found this way working Awesome. Which is also slightly similar to Mr.AndyFaizan,

  1. Check any of your xml files have errors or not.if yes R might not generate.

2.Fix the error at XML files. Then R will be generated automatically

  1. Then import the R which is suitable for your package.

  2. YES, your error "R cannot be resolved" Gone!!

Upvotes: 0

user3097695
user3097695

Reputation: 1274

The problem was caused by incompatibility of version. Here are the steps to fix the problem:

  1. Right click the project properties.
  2. Select Android
  3. Select correct Project build target. I fixed mine.

Upvotes: 0

user3024897
user3024897

Reputation: 1

Okay so I had the same problem, and I also tried everything: Check for bad naming, remove the import statement, installed the Android SDK Build-tools, check the AndroidManifest.xml, reinstall eclipse and android SDK etc. nothing worked.

And I was even working with a completely blank project (new -> android project)

I then found out that the android SDK comes with an already zipped eclipse, so I thought I might as well try that one.

So I uninstalled eclipse 64bit, and used the one which comes with the android SDK (http://developer.android.com/sdk/index.html#win-bundle) and now I dont get the error anymore, so give it a try.

I also found that using eclipse 64bit with a java 32bit version might cause problems. So it might be that the one that comes with the Android SDK vertion is the 32bit eclipse.

Hope this helps.

Upvotes: 0

Rehan
Rehan

Reputation: 14

Try this once

Go to Project

Check Mark "Build Automatically"

and Restart Eclipse

Upvotes: 0

yogagunda
yogagunda

Reputation: 11

I had the same problem.

The R.Java was not generated(in the gen folder).

There was no error in the xml file.

The issue was that I did not update the sdk completely.

In the SDK manager we need to install the package whenever the "install(number) packages" button is enabled and close and open the sdk manager to check for updates.

Close and reopen eclipse and check for updates in eclipse also.

Upvotes: 1

sardonicus87
sardonicus87

Reputation: 105

Did you start the project a while back and recently it has done this when you went to work on it? I had the same issue and recently came to find an answer. It could be that the problem is you need to go into the SDK manager and install a particular update as per the answer to this question: Eclipse error: R cannot be resolved to a variable

Upvotes: 0

rahul
rahul

Reputation: 2661

just try this at your imports

    import your_packgae_name.R;

Upvotes: 4

sonali8890
sonali8890

Reputation: 41

If you are getting same problem when creating new project then the problem is with your SDK Version.

Update your SDK to rev. 22.0.1 After updating you will find new tool in SDK manager that is android SDK build tools.

thn install that tool also.

Hope this will help you.

Upvotes: 2

  1. Delete the imported android.R (As what you did)
  2. Re-import R
  3. Eclipse will allow you to choose which should be imported. Choose the R from your package.

Upvotes: 1

GaelDev
GaelDev

Reputation: 85

Right click on the project's folder and then, Android Tools --> Run Lint - check for commons error. When this don't work, disable automated build, clean projet, run lint again --> build.

Ah also, in the proprierties -> Android, verify that build target is checked...

Upvotes: 1

Ann ann
Ann ann

Reputation: 21

check your drawables and make sure there is no file with capital letters to avoid error! .

Upvotes: 0

matreshkin
matreshkin

Reputation: 2208

Check if file "R.java" exists in folder "gen"

When aapt can not compile the resources, the R class can not be created. Look for errors in the resources of your Application.

More output you can find in the ErrorLog and Console window

(Eclipse Window menu->show view->Console (and choose "Android" in Console popup menu)).

Upvotes: 0

AndyFaizan
AndyFaizan

Reputation: 1893

One of your xml files might have errors. Hence, R is not generated. Fix them and R will be generated and the errors will be removed.

Upvotes: 0

Aerrow
Aerrow

Reputation: 12134

Make sure this line,

setContentView(R.layout.activity_main);

in your code it contains "q".

Then build your application and then run

Upvotes: 0

Related Questions