Java:找不到符号变量

这是我的课程的复制/粘贴作业,但是无论我尝试什么,我都遇到此“错误:找不到符号变量total_pizzas”。我试图无效并重新启动,清理项目,甚至删除了.iml文件和.idea文件夹。我目前不知道该怎么办。

我也尝试过重命名有问题的变量以进行匹配,但是无论如何它仍然会抛出该错误。我还要补充一点,这是在Android Studio中。

下面是代码(添加了XML):

这是我遇到的问题所在的行:

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>
jdwcjs 回答:Java:找不到符号变量

您很可能没有将字符串“ total_pizzas”添加到res / values / string.xml文件中。

打开res / values / string.xml文件,并确保其外观如下。

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

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

app_name很可能已经声明了,唯一需要添加的部分是

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

您自己的文本,您需要更改为所需的任何内容。

本文链接:https://www.f2er.com/3169503.html

大家都在问