Search code examples
javaandroidxml

findViewById(R.id.activity_main) --> cannot resolve symbol 'activity_main'


!! Please note !! The error does not occur in the call to the setContentView() method. While searching for answers I found that someone posted here with the exact same problem (exact same code from presumably the exact same tutorial source and everything), but it was marked as a duplicate and erroneously directed to a post where 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 is not the case here. For the record I tried that anyway but it just changed the error message to "hey, this needs to be 'id'"!

=== PROBLEM ===
Currently the only 2 errors in my code both point to identical statements in different methods
RelativeLayout bgElement = findViewById(R.id.activity_main);
where activity_main is red with message "cannot resolve symbol 'activity_main'"
When cleaning and rebuilding the compile error is: error:
cannot find symbol variable activity_main

This is my first android programming project and I've never used xml either, so please talk slow and use small words. lol

=== RESEARCH / ATTEMPTED FIXES ===
1) import android.R has never been in my code.
2) Cleaning and rebuilding does not fix it. (I clean and rebuild after every attempted fix)
3) The example code I'm following had type cast in front of the method call that Android Studio warned was redundant, so I deleted it. Then one post suggested the casting was necessary so I tried adding it back. The error remains when the casting is there and is not.
4) One person said to try deleting the R file before rebuilding, but there is no R.java where they said it was - maybe it was referring 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

  • You're trying to find a layout that doesn't exist in your xml file:

    RelativeLayout bgElement = /*(RelativeLayout)*/findViewById(R.id.activity_main);
    

    No element in your xml file has an id "activity_main" nor do you have a RelativeLayout in your xml.

    Mostly the error comes from the lack of an element with an id set to activity_main.

    I suppose you wanted to change the background color of the entire screen, so add an id to the ConstraintLayout:

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

    And then change the code to work with that:

    ConstraintLayout bgElement = /*(ConstraintLayout)*/findViewById(R.id.activity_main);