Nicolas
Nicolas

Reputation: 99

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?

Upvotes: 7

Views: 44980

Answers (12)

Gladys Sanchez
Gladys Sanchez

Reputation: 41

You can clean o rebuild your project in

  • Build > Clean Project or Build > Rebuild Project

or it can be for the tools, trying install Android SDK Build Tools from

  • Tools > Android > SDK Manager.

Upvotes: 1

Gundu Bandgar
Gundu Bandgar

Reputation: 2603

Just go to Android Top menu list. click on Build Menu, in under Build click on Rebuild Project.

enter image description here

Upvotes: 0

DroidCrafter
DroidCrafter

Reputation: 136

Same problem.

It was the xml. Didn't even show an error, but when I built the project again a couple of errors showed up in the Messages display at the bottom of Android Studio.

It turns out that because I was using a GridLayout my rowCount and columnCount were set up wrong at the top of the page: app:columnCount="15" app:rowCount="15"

They had to be set up like:

android:columnCount="15"
android:rowCount="15"

Upvotes: 0

jdkimber
jdkimber

Reputation: 1

I had the same problem when I ran updates. I first tried to sync project with gradle files, but that did not work for me. What did work was that I went to:

File -> Project Structure -> Modules: app (left column) -> Properties tab.

Under that tab you have both:

"Compile Sdk Version" "Build Tools Version"

I think one was version 23 or 22 and the other was version 21. Don't remember exactly. All I remember is that they didn't match. So I made them match. I set the "Compile Sdk Version" to 23 and the "Build Tools Version" to 23.0.1 and the problem was solved. So use which ever you want, I guess, 23 and 23, 22 and 22, etc.

Upvotes: 0

Madhu
Madhu

Reputation: 5

Step 1: go to Build -> Clean Project

Step 2: Build > Rebuild Project

Upvotes: 0

NomanJaved
NomanJaved

Reputation: 1380

There are few points that you need to check.

  • android sdk manager need updates

or

  • Build > Clean Project.

or

  • Tools > android > Sync project with gradle files

Any one of them will solve the issue. Other wise you have error in you xml file that can not be configure with main java file. So check that error and solve it and then run the project. Hope this will help you.

Upvotes: 1

Omar Amil
Omar Amil

Reputation: 1

just do the following : Build > Clean Project then Build > Rebuild Project

Upvotes: 0

Nicolas
Nicolas

Reputation: 99

Thank you all for the answers! As you said, the solution was make a clean & rebuild and checked the SDK version. Now working pretty fine

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

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!

Upvotes: 8

A Honey Bustard
A Honey Bustard

Reputation: 3493

Put this in your /app/build.gradle file :

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile 'com.android.support:appcompat-v7:18.0.0'
}

and make sure the version code (here 18.0.0) is not higher than the buildToolsVersion in the same file :

android {
    compileSdkVersion 16
    buildToolsVersion "18.0.0"
}

then rebuild your project. Hope that helps! Oh and make sure you have the right buildTools installed via SDK Manager! Actual version is 21.0.2 I think.

Or maybe you need to download another API version in the SDK Manager (check Version in build.gradle) if you dont habe the right one/ or change your SDK Version in the build.gradle file.

Upvotes: 0

user1397215
user1397215

Reputation: 321

Check your Manifest if the attribute

package="com.example.yourpackagename"

Is correct

Other attributes must be correct as well, e.g. activity names corresponding to correct src files.

Upvotes: 2

BredeBS
BredeBS

Reputation: 198

in Android Studio go to

Build > Clean Project

then Build > Rebuild Project

Upvotes: 2

Related Questions