• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Cannot resolve symbol 'R'

Like the title says, the symbol 'R' cannot be resolved. I am using android studio.

The code:
Java:
package me.karkatsanger.insertname;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;




public class MainActivity extends AppCompatActivity {

    private Button button2;

    private void init() {
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mail = new Intent(MainActivity.this, Mail.class);

                startActivity(mail);

            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        init();
    }


    public void preform_action(View v) {
        Random rand = new Random();

        int minNumber = 1;
        int maxNumber = 10;
        int randomNumber = rand.nextInt((maxNumber - minNumber) + minNumber) + minNumber;
        TextView tv = (TextView) findViewById(R.id.facts);

        if (randomNumber == 1) {
            tv.setText("Number One");
        }else if (randomNumber == 2) {
            tv.setText("Number Two");
        }else if (randomNumber == 3) {
            tv.setText("Number Three");
        }else if (randomNumber == 4) {
            tv.setText("Number Four");
        }else if (randomNumber == 5) {
            tv.setText("Number Five");
        }else if (randomNumber == 6) {
            tv.setText("Number Six");
        }else if (randomNumber == 7) {
            tv.setText("Number Seven");
        }else if (randomNumber == 8) {
            tv.setText("Number Eight");
        }else if (randomNumber == 9) {
            tv.setText("Number Nine");
        }else {
            tv.setText("Number Ten");

        }


    }
}

My xml looks like this:

Java:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="me.karkatsanger.insertname.MainActivity"
    android:background="@drawable/ohh">

    <Button
    android:id="@+id/factsButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Tap me for facts"
    android:background="#"
    android:textColor="#F20"
    android:onClick="preform_action"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#"
        android:textColor="#F20"
        android:text="Any other quesitons?"
        tools:layout_editor_absoluteX="98dp"
        tools:layout_editor_absoluteY="447dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <TextView
        android:id="@+id/facts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="20sp"
        android:textColor="#F20"
        tools:layout_editor_absoluteY="247dp"
        tools:layout_editor_absoluteX="163dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>




</android.support.constraint.ConstraintLayout>


So I just rewrote the entire thing, cause it was basically just the main class and a little bit more, and now there are no errors.
 
Last edited:
You have a project build problem. Check the log for errors. It's probably a layout issue.
 
You have a project build problem. Check the log for errors. It's probably a layout issue.

Umm, I posted here because I needed help with what the error could be. I have tried to clean the build and rebuilt the apk. I literally posted the layout file so that I could get help and see what the problem is. As well as the code.

If I knew what steps to take to maybe solve the issue I wouldn't have posted here. So your answer does nothing in they way of helping me.

And the log just says that there is an error and it can't build the apk. I was searching for around 30 minutes yesterday and found nothing that helped.

If anyone could help me with this that would be amazing.

Thanks!
 
Umm, I posted here because I needed help with what the error could be. I have tried to clean the build and rebuilt the apk. I literally posted the layout file so that I could get help and see what the problem is. As well as the code.

If I knew what steps to take to maybe solve the issue I wouldn't have posted here. So your answer does nothing in they way of helping me.

And the log just says that there is an error and it can't build the apk. I was searching for around 30 minutes yesterday and found nothing that helped.

If anyone could help me with this that would be amazing.

Thanks!

Maybe if you posted something useful, like a screen capture of the error it would be easier. What you've posted there makes it impossible to diagnose the problem. So I gave a general answer because your question was so vague.
To give a more detailed explanation, the 'R' class is an auto-generated artefact of the build process. You don't write that code, it's generated automatically from the layouts you've defined in XML. I would look closely at all your layout XML files. Android Studio will clearly highlight any errors which are present.
Another thing you can try is to do a clean build of the project. This will force Android Studio to delete any previously created artefacts, and compiled class files, and regenerate everything.
And next time someone tries to help you, don't come back with such a shitty attitude, or next time, chances are you'll get no help whatsoever.
 
Maybe if you posted something useful, like a screen capture of the error it would be easier.

The thing is, the error only is "Cannot resolve symbol 'R'"

When I try to build the apk it just says that it cannot build because there are errors in the code.
 
To give a more detailed explanation, the 'R' class is an auto-generated artefact of the build process. You don't write that code, it's generated automatically from the layouts you've defined in XML. I would look closely at all your layout XML files. Android Studio will clearly highlight any errors which are present.
Another thing you can try is to do a clean build of the project. This will force Android Studio to delete any previously created artefacts, and compiled class files, and regenerate everything.

Also, I forgot to mention in the post, that I have tried to clean the project multiple times. I do it every time I change anything, just to be sure.

I have also looked through my XML files and I can't find any errors in there. No typos or anything.

Edit: I have also tried to sync the gradle scripts, as some people on other forums said that would help. I've done all the things that people recommend and I am really lost as to what I can do to solve this.
 
Actually it's much simpler than that. Your list of import statements at the top of the file does not include the 'R' class.

Hit Ctrl-Shift-O to update the import statements.
 
Back
Top Bottom