Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Why are my R in my code are in red saying cannot resolve symbol R?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_facts);

        // Declare our View variables and assign the the Views from the layout file
        final TextView factLabel = (TextView) findViewById(R.id.factTextView);
        Button showFactButton = (Button) findViewById(R.id.showFactButton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // the button was clicked so update the fact label with a new fact
                String fact = "Ostriches can run faster than horses.";
                factLabel.setText(fact);
                )
            };
            showFactButton.setOnClickListener(listener);

It is only the R give me problem and app won't run.

Hey Travis, Could you post your xml for your device layout? Additionally, are all of the R.<somethings> showing up red? Thanks.

Justin,

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=".FunFactsActivity"
    android:id="@+id/"
    android:background="#ff51b46d">

    <TextView
        android:text="Did you know?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/"
        android:textSize="24sp"
        android:textColor="#80ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_below="@+id/"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ants stretch when they wake up in the morning"
        android:id="@+id/fact textView3"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textColor="@android:color/white" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Another Fun Fact"
        android:id="@+id/show fact button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white" />


</RelativeLayout>

Justin,

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=".FunFactsActivity" android:id="@+id/" android:background="#ff51b46d">

<TextView
    android:text="Did you know?"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/"
    android:textSize="24sp"
    android:textColor="#80ffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_below="@+id/"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ants stretch when they wake up in the morning"
    android:id="@+id/fact textView3"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="24sp"
    android:textColor="@android:color/white" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Another Fun Fact"
    android:id="@+id/show fact button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white" />

</RelativeLayout>

Justin,

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=".FunFactsActivity"
    android:id="@+id/"
    android:background="#ff51b46d">

    <TextView
        android:text="Did you know?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/"
        android:textSize="24sp"
        android:textColor="#80ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_below="@+id/"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ants stretch when they wake up in the morning"
        android:id="@+id/fact textView3"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textColor="@android:color/white" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Another Fun Fact"
        android:id="@+id/show fact button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white" />


</RelativeLayout>

6 Answers

Hi,

Sometimes the Android Studio got some terrible, try to clean your project and rebuild it.

+1 what David has said here (Moved this comment to an Answer) - Android Studio does this sometimes so a Project Clean should fix the issue for you.

If not, I would try deleting the R class in your gen directory - it will rebuild itself when you next run a Make project rebuild.


Hopefully this should fix the issue but if not, let us know and we'll see what else can be done :)

Hey Travis, If you take a look at your XML document you will notice that you have the following id's for your graphical components.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_below="@+id/" 
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2" <!--Id #1-->
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ants stretch when they wake up in the morning"
    android:id="@+id/fact textView3"<!--Id #2-->
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="24sp"
    android:textColor="@android:color/white" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show Another Fun Fact"
    android:id="@+id/show fact button" <!--Id #3-->
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white" />

Let's consider each of the id's that I have commented above:

  • Id #1 : This is a valid id value. However, you may want to change this to be more descriptive. Instead of textView2 maybe you could use factLabelTextView. You should know what the component does or represents from the id name.
  • Id #2 and id #3 : I notice that you have a space in these id's. This doesn't play nice in the Java code. For instance, change
android:id="@+id/fact textView3"

to

android:id="@+id/factTextView3"

Once you have made those changes, you can then grab a reference in the Java code using something like the following:

final TextView factTextView = (TextView) findViewById(R.id.factTextView3);

After looking through these things, I believe that you can make your code compile and run. Hope everything works out.

Good Luck!

+1 Justin is correct here - the ID's you have provided in your XML code have spaces in and XML will not allow you to use these ID's.

Harry and David, interestingly enough , I had to rebuild and reinstall my ADV because initially it did not come with the R class as part of the Android Studio pkg. (make sense)? Therefore I am not sure if I should delete the R class out of my general directory. Advise.

I would still delete the R class as it should definitely rebuild itself.

If you are worried that it might not, right-click on the R class and click Show in Explorer. Then, back the file up somewhere before you delete the class.


Let me know how it goes :)

Ok. This is what I have tried so far to rid myself of the R , highlight and Alt+Enter, but it gives me nothing to go on. Where or What is the R class in the Gen Directory?

I now have a whole series of errors that are showing up my XML, after making corrections. The error is still cannot resolve on the R itself;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fun_facts);

        // Declare our View variables and assign the the Views from the layout file
        final TextView factLabel = (TextView) findViewById(R.id.factTextView);
        Button showFactButton = (Button) findViewById(R.id.showFactButton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // the button was clicked so update the fact label with a new fact
                String fact = "Ostriches can run faster than horses.";
                factLabel.setText(fact);
                )
            };
            showFactButton.setOnClickListener(listener);
};

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.fun_facts, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Hey Travis, Try to have Gradle Clean and Rebuild your project. That can be accomplished using the Build --> Gradle Clean then using the Gradle Build. That may resolve your issues. If that doesn't work then we need to look at additional issues. Hope that helps.