Java – findViewById(R.id.activity_main) –> Unable to parse symbol ‘activity_main’

findViewById(R.id.activity_main) –> Unable to parse symbol ‘activity_main’… here is a solution to the problem.

findViewById(R.id.activity_main) –> Unable to parse symbol ‘activity_main’

!! Please note!! Errors do not occur in the invocation of the setContentView() method. While searching for the answer, I found that someone posted the exact same question here (the exact same code could be from the exact same tutorial source and everything), but it was flagged as a duplicate and incorrectly directed to the post that the problem was a mismatch type in the setContentView() method instead of findViewByID(), and the solution was to change “R.id.activity_main” to “R.layout.activity_ main”, but that’s not the case here. For the record, I tried anyway, but it just changed the error message to “Hey, this needs to be ‘id’!”

=== Question ===
The only 2 errors in my code right now point to the same statement
in different methods
RelativeLayout bgElement = findViewById(R.id.activity_main);
where activity_main is red and displays the message “Unable to resolve symbol ‘activity_main'”
The compilation error on cleanup and rebuild is: error:
Symbolic variable activity_main could not be found

This is my first Android programming project, and I’ve never used XML before, so please slow down and use small words. Haha

=== Research/try to fix ===
1) import android. R never appears in my code.
2) Cleaning and rebuilding does not solve the problem. (I clean up and rebuild after every attempt to repair)
3) I did the type conversion below code before the method call that Android Studio warned, warning it to be redundant, so I removed it. Then a post suggested that type conversion was necessary, so I tried adding it back. The error persists when there is and when no conversion exists.
4) Someone says try to delete R files before rebuilding, but they say no R .java somewhere – maybe it refers to an older version of Android Studio?

====Java code===

package com.example.app1redbluelight;

import android.support.v7.app.AppCompatActivity;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

Button button;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout bgElement = /*(RelativeLayout)*/findViewById(R.id.activity_main);
        bgElement.setBackgroundColor(Color.WHITE);
        myButtonListenerMethod();
    }

public void myButtonListenerMethod() {
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout bgElement = /*(RelativeLayout)*/findViewById(R.id.activity_main);
                int color = ((ColorDrawable) bgElement.getBackground()).getColor();
                if (color == Color.RED)
                    bgElement.setBackgroundColor(Color.BLUE);
                else
                    bgElement.setBackgroundColor(Color.RED);
            }
        });
    }
}

=== XML file ===

<?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=". MainActivity">

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="148dp"
        android:layout_marginStart="148dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="231dp" />
</android.support.constraint.ConstraintLayout>

Solution

In Android, the function findViewById() returns the View corresponding to the id in the layout.
In your code, setContentView(R.layout.activity_main); Sets the layout of the main activity.
At this point, findViewById() finds the view in the activity_main layout.
So it can’t parse the “activity_main” in the activity_main layout.
If you need to get the layout for activity_main, try the following.

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  ConstraintLayout bgElement = (ConstraintLayout)inflater.inflate(R.layout.activity_main, null);

If you need

to get the layout in the activity_main.xml, you need to set the ID of the layout. (You must add android:id = “@+id/activity_main”.) )
See below.

<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"
android:id = "@+id/activity_main"
tools:context=". MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="148dp"
    android:layout_marginStart="148dp"
    android:text="Button"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="231dp" />
</android.support.constraint.ConstraintLayout>

Related Problems and Solutions