我刚开始研究Android Development Tutorial on youtube。当我实现CrazyTipCalc.java时,Eclipse报告了以下错误:
menu cannot be resolved or is not a field CrazyTipCalc.java /CrazyTipCalc/src/com/example/crazytipcalc line 107 Java Problem
以下是CrazyTipCalc.java的代码,其中最后6行具有“R.menu未解决”问题。我在构建项目之前导入了com.example.crazytipcalc.R。为什么会出现此问题以及如何解决?谢谢。
package com.example.crazytipcalc;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import com.example.crazytipcalc.R;
//import android.R;
public class CrazyTipCalc extends Activity {
public static final String TOTAL_BILL = "Total bill";
public static final String CURRENT_TIP = "Current tip";
public static final String BILL_WITHOUT_TIP = "Bill without tip";
private double billBeforeTip;
private double tipAmount;
private double finalBill;
EditText billBeforeTipET;
EditText tipAmountET;
EditText finalBillET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crazy_tip_calc);
if(savedInstanceState == null) {
billBeforeTip = 0.0;
tipAmount = .15;
finalBill = 0.0;
} else {
billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
tipAmount = savedInstanceState.getDouble(CURRENT_TIP);
finalBill = savedInstanceState.getDouble(TOTAL_BILL);
}
billBeforeTipET = (EditText) findViewById(R.id.billEditText);
tipAmountET = (EditText) findViewById(R.id.tipEditText);
finalBillET = (EditText) findViewById(R.id.finalBillEditText);
tipSeekBar = (SeekBar) findViewById(R.id.changeTipSeekBar);
tipSeekBar.setOnSeekBarChangeListener(tipSeekBarListener);
billBeforeTipET.addTextChangedListener(billBeforeTipListener);
}
private TextWatcher billBeforeTipListener = new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
try{
//Change the billBeforeTip to the new input
billBeforeTip = Double.parseDouble(arg0.toString());
}
catch(NumberFormatException e) {
billBeforeTip = 0.0;
}
updateTipAndFinalBill();
}
};
private void updateTipAndFinalBill() {
double tipAmount = Double.parseDouble(tipAmountET.getText().toString());
double finalBill = billBeforeTip + (billBeforeTip * tipAmount);
finalBillET.setText(String.format("%.02f", finalBill));
}
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putDouble(TOTAL_BILL, finalBill);
outState.putDouble(CURRENT_TIP, tipAmount);
outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
}
//End of first part
//Second Part
//SeekBar used to make a custom tip
private SeekBar tipSeekBar;
private OnSeekBarChangeListener tipSeekBarListener = new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
tipAmount = (tipSeekBar.getProgress()) * 0.01;
tipAmountET.setText(String.format("%.02f", tipAmount));
updateTipAndFinalBill();
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {}
public void onStopTrackingtouch(SeekBar arg0) {}
};
//End of second part
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.crazy_tip_calc, menu);
return true;
}
}
答案 0 :(得分:3)
检查res / menu文件夹中是否有crazy_tip_calc.xml。
在此之后,尝试一个项目> clean