Android Studio: can not resolve symbol "R"

Nicolas picture Nicolas · Dec 2, 2014 · Viewed 44.9k times · Source

I know this question has been asked several times, but no answers are useful due to my context. The thing is, I just opened my first project (in my actual computer) and without editing anything all the "R." in the mainActivity.java turn red. Do anyone know what are the causes for this problem and how it can be fixed?

Here is the .java main activity

package com.nico.calc_02;
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.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        } 

        return super.onOptionsItemSelected(item);
    }
}

and this is the .xml layout:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

I haven't modified the code or anything. I'm using Android Studio. Does anyone know how to fix it?

Answer

BlackHatSamurai picture BlackHatSamurai · Dec 2, 2014

There are a couple of things that it could be:

1. You may need to do a clean & build
2. You may need to update the SDK (Go into your SDK manager, and make sure that all your files are up to date)

I find that this happens mostly when I update AS, and when you do the AS update, you also need to update the SDK via the SDK manager. Be sure that your tools are also up to date, and not just the API. Things like com.android.support:support and com.google.android.gms:play-services will cause this error. Check the Run tab at the bottom left, as that should give you an idea if something is out of date, and you need to update. If you are using libraries, they may have been updated as well.

You also want to check your build.gradle file inside your mobile directory and make sure that the dependancies are inline with your SDK via the SDK manager. This is usually the culprit for me, especially when I update AS.

Unfortunately, there is no "easy" solution for this. You are just going to have to try different things until you find it, because it could be as simple as doing a "clean & build".

Good luck!