Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R.id.main giving me a red error, cannot resolve symbol main in MainActivity.java

I’m using Android Studio for the first time. I’m following a tutorial online to build a calculator, and I’m trying to run my program through the emulator. But when I run it, I get the error message, "Cannot find symbol variable id".

I read through a lot of questions and answers trying to find the exact problem, but the closest I found was that in "R.id.main", the ‘main’ part should be somehow tied to my activity_main file, but I don’t know how. I know some were saying to use "android:id" but in the tutorial, the instructor hadn’t done anything with ids yet. So I don’t know if it’s because I somehow saved a file in a wrong folder.

I tried doing a Clean Build as I saw in one solution to someone else’s related issue, but it didn’t help. The program just gave the same error. Here is the compiler error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

C:\Users\using\Desktop\Android Projects\BMICalculator\app\src\main\java\com\miramichelle\bmicalculator\MainActivity.java:18: error: cannot find symbol
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
                                                                ^
  symbol:   variable id
  location: class R

My code is:

MainActivity.java

package com.miramichelle.bmicalculator;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BMI Calculator" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female" />

    </LinearLayout>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Age" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Feet" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Inches" />
    </LinearLayout>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Weight (kg)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="calculate" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="23.6" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You are a healthy weight!" />
</LinearLayout>

>Solution :

In the layout file (activity_main.xml) you should set android:id="@+id/main" to a view you’re interested in, most likely for LinearLayout.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading