1

When I try to create an android project in eclipse the code has errors.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

I'm getting "R cannot be resolved to a variable"

I think it can't detect the resources?

My SRC code is:

package com.android.h4;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

   @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.main, menu);
       return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
    }
}

In all of R eclipse detects an error. I tried to create a new project with appcompat_v7 and I deleted the libs and res folders as well as the AndroidManifest.xml. Then i imported these folder from a working project with the same name(someone emailed them to me) and at emptied the src but the error remains.

Any idea what I should do?

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
bety
  • 21
  • 4
  • 1
    check your xml files.... some where in xml file has error – RajaReddy PolamReddy May 20 '15 at 10:13
  • This is caused by an error somewhere in your project (usually the XML) that wont let Eclipse build the automatically generated R file properly. Check your XML and there's likely an error somewhere in there. – SuperBiasedMan May 20 '15 at 10:19
  • i started android yesterday so i dont know what thing it must look likes to ?please help me thanks – bety May 20 '15 at 10:22
  • If *R* is not detected then you must check the spellings of the **ID**.there must be some typo... – Suraj Palwe May 20 '15 at 10:37
  • If you have a string with an apostrophe in strings.xml or you copy an image to drawable with a capital letter or have a space in the image name it can cause this type of error. – SmulianJulian May 20 '15 at 10:54
  • i upload a png file that contains a picture of my AndroidManifest.xml so i will pleased if you check it and answer me is it right? the link of it is http://cld.persiangig.com/download/9I7oM8Nhhr/Untitled1.png/dl – bety May 20 '15 at 11:03

2 Answers2

0

There are some possibilities that may evoke that error.

  1. The easiest way (seems stupid but often helps) Go to "Project" in the Menu of Eclipse and click on "Clean" After Cleaning, the workspace is rebuild and hopefully the R-file is recognized than. (Sometimes even a restart of Eclipse helps)
  2. Check all your IDs. As already said in comments, it's possible that you have misspelled the ID (you define your IDs in the layout, there you have to check it)

Last but not least: DON'T TRY TO CHANGE THE R-FILE YOURSELF! :)

I hope I could help you. If not, just specify your problem (i.e send a copy of your layout xml)

user2550641
  • 317
  • 4
  • 18
0

You should clean your project and build again. For doing that you should go to

tab project -> clean -> select the project

And for more details you can also dig out on these links

R cannot be resolved - Android error

“R cannot be resolved to a variable”? duplicate

Community
  • 1
  • 1
BBdev
  • 4,898
  • 2
  • 31
  • 45