Android Studio 2.1 Cannot resolve symbol 'R'

24,544

Solution 1

I try to change the Gradle version from 2.1.0 to 2.0.0, even in AS ver 2.1, ignoring the Instant Run performance and its work out. The little R error is now gone. Now, I tried to change back to 2.1.0, the R error also gone..!! Sometime this little thing drive me crazy, a bit.

Shame on me.

Solution 2

Try syncing the project, go to Tools > Android > Sync Project with Gradle Files

UPDATE

If you're using Android Studio 3.3, use the Sync Project with Gradle Files icon

enter image description here

Solution 3

I have just gone through this nightmare. Ended up removing all xml code to notepad until it was fixed. Then reintroduced sections of code until problem appeared. Thinking back, it all started when I pasted some code using WordPad. Apparently WordPad contains a different coding or something so bet to avoid. Anyway just check your white space in xml by deleting it all to rid yourself of any hidden characters.

Solution 4

Try performing a clean and rebuild by going to Build > Clean Project and Build > Make Project

Solution 5

I've seen this problem many times. The right solution is:

When you type R, the Android Studio will give you many different namespaces for R to choose from, that look like R(***.***.***), but what you really need is the namespace that ends with ***.myProject. (Note: myProject is your project name), e.g: R(com.example.milka.demoProject).

If you choose any other namespace, you will get the error message, so you have to delete the unneeded import, and re-choose your R

Share:
24,544
Infinite Loops
Author by

Infinite Loops

I'm an Android Developer on my free time.

Updated on January 30, 2020

Comments

  • Infinite Loops
    Infinite Loops over 4 years

    Android Studio 2.1, Java V8 Update 91, Gradle 2.1.0 .

    I know that this questions has been asked many times here, but none of the recommendation solutions works out.

    In a sudden after compiling ready to run on Emulator, my IDE give an error at the R.id attribute. Every Java files suddenly give the same error even I don't make any changes to the file.

    I tried to make a new project, see if things different but the error keep coming. In my mind it could be internal error, again. Even I just update my IDE just now.

    The Java file;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_science);
    
        list_View = (ListView) findViewById(R.id.maintable); 
        String[] Days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
        dayList.addAll(Arrays.asList(Days));
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
        list_View.setAdapter(adapter);
    
        NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
    
        if (nfc.isEnabled()) {
            adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dayList);
            Toast.makeText(this, "NFC turned ON", Toast.LENGTH_SHORT).show();
        }
    
        else {
            Toast.makeText(this, "Please turn On NFC", Toast.LENGTH_SHORT).show();
        }
    
    }
    

    The R is the error.

    XML file that associate to the Java;

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/maintable"
        >
    </ListView>
    

    Is this, again, an internal error?

  • Omar Aflak
    Omar Aflak about 8 years
    Try also to clean the project: Build > Clean Project. If the error persist, then you probably have a syntax error in one of your XML files.
  • Infinite Loops
    Infinite Loops about 8 years
    I've done that, in fact the project synced when we start the IDE.
  • Hadas Kaminsky
    Hadas Kaminsky about 8 years
    @InfiniteLoops Does sync fail? Do you get any useful error message?
  • Infinite Loops
    Infinite Loops about 8 years
    It says, Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed with multiple errors, see logs
  • CommonsWare
    CommonsWare about 8 years
    @InfiniteLoops: That would be why your R class is not being generated.
  • Hadas Kaminsky
    Hadas Kaminsky about 8 years
    @InfiniteLoops Try this one: stackoverflow.com/questions/17587751/…
  • Infinite Loops
    Infinite Loops about 8 years
    still, didn't resolve. Still says the error like the one I reply above.
  • Infinite Loops
    Infinite Loops about 8 years
    Sorry but I'm not using GPService.
  • Infinite Loops
    Infinite Loops almost 8 years
    Nope. I did clean, rebuild, and sync... Look at my own answer below.