gpt4 book ai didi

java - 无法在 Eclipse 中解析 R.menu

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:42 25 4
gpt4 key购买 nike

我刚开始研究 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 not resolved”的问题。我在构建项目之前导入了 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;
}
}

最佳答案

检查 res/menu 文件夹中是否有 crazy_tip_calc.xml。

在此之后,尝试一个项目>clean

关于java - 无法在 Eclipse 中解析 R.menu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27570001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com