7

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?

GSee
  • 45,014
  • 13
  • 114
  • 135
Nicolas
  • 99
  • 1
  • 2
  • 6
  • Try this [solution][1] it may resolve your problem [1]: http://stackoverflow.com/a/32119880/3343174 – Fakher Aug 20 '15 at 14:02
  • Try this [solution][1] it may resolve your problem. [1]: http://stackoverflow.com/a/32119880/3343174 – Fakher Aug 20 '15 at 14:03

12 Answers12

8

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!

BlackHatSamurai
  • 21,845
  • 20
  • 84
  • 147
2

in Android Studio go to

Build > Clean Project

then Build > Rebuild Project

BredeBS
  • 196
  • 8
2

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.

user1397215
  • 177
  • 1
  • 2
  • 17
1

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.

NomanJaved
  • 1,038
  • 11
  • 27
1

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.
0

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.

A Honey Bustard
  • 3,142
  • 2
  • 19
  • 36
0

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

Nicolas
  • 99
  • 1
  • 2
  • 6
0

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

0

Step 1: go to Build -> Clean Project

Step 2: Build > Rebuild Project

Madhu
  • 5
  • 6
0

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.

jdkimber
  • 1
  • 2
0

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"
DroidCrafter
  • 136
  • 1
  • 4
0

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

enter image description here

Gundu Bandgar
  • 2,383
  • 1
  • 15
  • 20