Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: can not resolve symbol "R"

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?

like image 692
Nicolas Avatar asked Dec 02 '14 20:12

Nicolas


People also ask

What is r in Android Studio?

R is a class containing the definitions for all resources of a particular application package. It is in the namespace of the application package. For example, if you say in your manifest your package name is com. foo. bar , an R class is generated with the symbols of all your resources in com.

Where is the R Java file in Android Studio?

R. java is the generated file by ADT or Android studio. It will be located under app\build\generated\source\r directory.

What does Cannot resolve symbol mean in Java?

The cannot find symbol error, also found under the names of symbol not found and cannot resolve symbol , is a Java compile-time error which emerges whenever there is an identifier in the source code which the compiler is unable to work out what it refers to.


1 Answers

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!

like image 177
BlackHatSamurai Avatar answered Oct 05 '22 23:10

BlackHatSamurai