1

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

}
3
  • looks like you need to clean your project! Make sure to fix your xml errors first. Commented Jun 7, 2013 at 9:19
  • Did you recenlty update the ADT ? Commented Jun 7, 2013 at 9:31
  • it could be anything, in my project, R stopped resolving because I had images with uppercase letters in my res/drawable folder. I was not aware of this, renamed them with all lowercase and R suddenly worked/resolved :) Commented Feb 26, 2014 at 0:25

15 Answers 15

4

just try this at your imports

    import your_packgae_name.R;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

I got this Cannot complete the request. See the error log for details. "Android Traceview" will be ignored because it is already installed. "Tracer for OpenGL ES" will be ignored because it is already installed. "Android Development Tools" will be ignored because it is already installed. "Android Hierarchy Viewer" will be ignored because it is already installed. "Android DDMS" will be ignored because it is already installed.
open SDK manager directly and if update is available then update it.
1

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...

Comments

1
  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.

3 Comments

Theres just one option to import which says "import R(android) The rest are "create class" options
That's odd. Can you upload your project file somewhere?
Yeah sure I can do that. Where do I upload it?
1

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.

Comments

0

Make sure this line,

setContentView(R.layout.activity_main);

in your code it contains "q".

Then build your application and then run

4 Comments

No no it doesn't. That was just a typo. The main issue is that R.
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
@SaadRehman: Restart your eclipse ide, (this is silly trick but the R file will be generated)
0

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.

4 Comments

this should go to comments!
I just created a new project. How can it still have errors? I'm new to android and I don't know where the error in xml might be
@LazyNinja: Yes. I realise that now :/
@SaadRehman: There should be an activity_main.xml in your res/layouts folder. Open it to see if you've got errors. Refer any beginner's guide to android. You'll get to know what to do.
0

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)).

2 Comments

Where do I have to look for the error? I just created the new project!
Do: 1 Window menu->show view->Console 2.choose "Android" in Console popup menu. 3. Clean project (Project menu->check off "Build automatically") 4. Project menu->Clean... 5. Select your project, check on "Start build immediately"->OK. 6. Show errors in "Problems" window, and "Console" window
0

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

Comments

0

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

Comments

0

Try this once

Go to Project

Check Mark "Build Automatically"

and Restart Eclipse

Comments

0

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.

Comments

0

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.

1 Comment

I copy the project from office computer to a home computer. The project was set to Android 4.1.2. When it was imported to home computer, it was imported as 4.2.2.
0

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!!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.