-3

This is a copy/paste assignment for my class, but I am encountering this 'error: cannot find symbol variable total_pizzas' regardless of what I try. I have attempted to Invalidate and Restart, clean project, and even deleted the .iml files and .idea folder. I don't know what else to do at this point.

I have also tried renaming the variable in question to match, but it still throws that error regardless. I want to also add that this is in Android Studio.

The code is below (XML added):

this is the line I am having issues with:

String totalText = getString(R.string.total_pizzas); 
package com.example.pizzaparty;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText mNumAttendEditText;
    private TextView mNumPizzasTextView;
    private RadioGroup mHowHungryRadioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Assign the widgets to fields
        mNumAttendEditText = findViewById(R.id.attendEditText);
        mNumPizzasTextView = findViewById(R.id.answerTextView);
        mHowHungryRadioGroup = findViewById(R.id.hungryRadioGroup);
    }

    public void calculateClick(View view) {

        // Get how many are attending the party
        int numAttend;
        try {
            String numAttendStr = mNumAttendEditText.getText().toString();
            numAttend = Integer.parseInt(numAttendStr);
        }
        catch (NumberFormatException ex) {
            numAttend = 0;
        }

        // Get hunger level selection
        int checkedId = mHowHungryRadioGroup.getCheckedRadioButtonId();
        PizzaCalculator.HungerLevel hungerLevel = PizzaCalculator.HungerLevel.RAVENOUS;
        if (checkedId == R.id.lightRadioButton) {
            hungerLevel = PizzaCalculator.HungerLevel.LIGHT;
        }
        else if (checkedId == R.id.mediumRadioButton) {
            hungerLevel = PizzaCalculator.HungerLevel.MEDIUM;
        }

        // Show the number of pizzas needed
        PizzaCalculator calc = new PizzaCalculator(numAttend, hungerLevel);
        int totalPizzas = calc.totalPizzas();
        String totalText = getString(R.string.total_pizzas);
        mNumPizzasTextView.setText(totalText + " " + totalPizzas);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context="com.example.pizzaparty.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Number of people?"
        android:textSize="24sp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="5"
        android:id="@+id/attendEditText" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="How hungry?"
        android:textSize="24sp" />

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/hungryRadioGroup">
        <RadioButton
            android:text="Light"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lightRadioButton" />
        <RadioButton
            android:text="Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:id="@+id/mediumRadioButton" />
        <RadioButton
            android:text="Ravenous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ravenousRadioButton" />
    </RadioGroup>

    <TextView
        android:id="@+id/answerTextView"
        android:text="Total pizzas: ?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textSize="24sp"/>

    <Button
        android:text="Calculate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/calcButton"
        android:layout_marginTop="20dp"
        android:onClick="calculateClick" />
    <TextView
        android:id="@+id/name"
        android:text="Test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp" />
</LinearLayout>
Ferillius
  • 1
  • 1

1 Answers1

0

You have most likely not added the string "total_pizzas" to the res/values/string.xml file.

Open res/values/string.xml file and make sure it looks as following.

<resources>
    <string name="app_name">YOUR APP NAME</string>

    <string name="total_pizzas">YOUR OWN TEXT</string>
</resources>

app_name is most likely already declared and the only part you need to add is

<string name="total_pizzas">YOUR OWN TEXT</string>

YOUR OWN TEXT you will need to change to whatever you want.

OscarCreator
  • 374
  • 4
  • 13