Android代码报‘Class, Interface or Enum Expected’错误求助排查
Let's break down the small but critical issues causing that frustrating error in your code—they're easy to miss but simple to fix:
1. Typo in SeekBar Listener Import
You have a lowercase b in Seekbar here:
import android.widget.Seekbar.OnSeekBarChangeListener;
Android's class name is SeekBar (capital B), so correct the import to:
import android.widget.SeekBar.OnSeekBarChangeListener;
2. Mismatched Superclass Import & Declaration
You imported AppCompatActivity but your class extends Activity:
import android.support.v7.app.AppCompatActivity; // ... public class MainActivity extends Activity {
Since you're using the support library import, update the class declaration to match:
public class MainActivity extends AppCompatActivity {
If you intended to use the base Activity class instead, add the corresponding import:
import android.app.Activity;
3. Misplaced Listener Definitions & Extra Closing Braces
You defined your SeekBar.OnSeekBarChangeListener and TextWatcher inside the calculate() method, which is invalid syntax for Java—these need to be top-level members of your class. Your code also has an extra closing brace at the end that breaks the class structure entirely.
Corrected Full Code
Here's the fixed version with all issues resolved, plus small tweaks for modern Android Studio:
package com.example.chigh.tipcalculator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends AppCompatActivity { private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); private static final NumberFormat percentFormat = NumberFormat.getPercentInstance(); private double billAmount = 0.0; private double percent = 0.15; private TextView amountTextView; private TextView percentTextView; private TextView tipTextView; private TextView totalTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize views (no explicit cast needed in modern Android Studio) amountTextView = findViewById(R.id.amountTextView); percentTextView = findViewById(R.id.percentTextView); tipTextView = findViewById(R.id.tipTextView); totalTextView = findViewById(R.id.totalTextView); tipTextView.setText(currencyFormat.format(0)); totalTextView.setText(currencyFormat.format(0)); EditText amountEditText = findViewById(R.id.amountEditText); amountEditText.addTextChangedListener(amountEditTextWatcher); SeekBar percentSeekBar = findViewById(R.id.percentSeekBar); percentSeekBar.setOnSeekBarChangeListener(percentSeekBarListener); } private void calculate() { percentTextView.setText(percentFormat.format(percent)); double tip = billAmount * percent; double total = billAmount + tip; tipTextView.setText(currencyFormat.format(tip)); totalTextView.setText(currencyFormat.format(total)); } // Define SeekBar listener as a class member private final SeekBar.OnSeekBarChangeListener percentSeekBarListener = new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { percent = progress / 100.0; calculate(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }; // Define TextWatcher as a class member private final TextWatcher amountEditTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try { billAmount = Double.parseDouble(s.toString()) / 100.0; amountTextView.setText(currencyFormat.format(billAmount)); } catch (NumberFormatException e) { amountTextView.setText(""); billAmount = 0.0; } calculate(); } @Override public void afterTextChanged(Editable s) { } }; }
Quick Modernization Note
If you're using AndroidX (recommended for new projects), replace the support library import with:
import androidx.appcompat.app.AppCompatActivity;
内容的提问来源于stack exchange,提问作者Casey Hight

