这是我的课程的复制/粘贴作业,但是无论我尝试什么,我都遇到此“错误:找不到符号变量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>