简体   繁体   中英

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>

In Android , that function findViewById() returns the view for the corresponded to the id in that layout.
In your code, setContentView(R.layout.activity_main); sets the layout for the mainactivity.
At this time, findViewById() finds the view in the activity_main layout.
So it couldn't resolve 'activity_main' in the activity_main layout.
If you need to get the layout of the 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 layout in the activity_main.xml, you need to set the id of the layout. ( android:id = "@+id/activity_main" has to be added.)
See the following.

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

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

The issue is caused by these reasons.
1 : There is no id named "activity_main" in the attached xml.
The findViewbyId() finds the View in that xml file, because you set the activity_main.xml to the MainActivity.
2 : There is no RelativeLayout item in the xml content.

Fix
Try to add the id, "activity_main" to the ConstraintLayout,
or Try use LayoutInflator.

Found that calling activity_main in this way wasn't ideal and would cause an app crash due to the constraint layout.

Insert a new horizontal layout and then reference that layout to do the background color change. Android doesn't seem to like trying to change the variables of the constraint layer too much in this way.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM