Ashni Goyal
Ashni Goyal

Reputation: 827

"R cannot be resolved to a variable" android error

I have freshly installed eclipse 3.8 and android sdk. When I create a new project I get error R cannot be resolved to a variable in MainActivity.java. R.java is missing and instead I have BuildConfig.java in gen folder. Based on suggestions given on similar questions I tried followings but no success.

  1. Reinstalled ADT
  2. Cleaned the project
  3. Restart eclipse

Kindly help.

Upvotes: 1

Views: 11042

Answers (6)

Alvaro
Alvaro

Reputation: 2059

See this question Eclipse giving error, missing R.java file after recent update, probably help you if you recently installed the ADT

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 package" 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: 0

jtt
jtt

Reputation: 13541

If you have an error in your XML file then R.java will not be generated. Try cleaning your project and look for any signs of errors on your XML files.

Upvotes: 0

Rocel
Rocel

Reputation: 1037

if you are using Eclipse, remove the R class import in your class and press CTRL + SHIFT + O, it will automatically import your classes. If it proposes your move than one R class, select the one from your project.

Upvotes: 0

Mike Marshall
Mike Marshall

Reputation: 7850

Yes, XML files may be the culprit, but also make sure you have an import statement at the top of your file for it as well. Edit here is the typical beginning of a java file, you need the R import where my comment is

package your.package.name;

import your.package.name.R;    // make sure you have this line
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

Upvotes: 3

Diogo Moreira
Diogo Moreira

Reputation: 1082

Verify the syntax on your XML files. The problem may not be in MainActivity.java. If there's an error on any of your XML files the class R won't be generated.

Upvotes: 0

Related Questions