简体   繁体   English

使用Eclipse,我得到一个错误“变量”,该错误无法解决或不是字段,我不确定为什么

[英]Using Eclipse, I get an error “Variable” cannot be resolved or is not a field and I'm not sure why

It occurs in the MainActivity.java portion and there are no errors in any other folder. 它发生在MainActivity.java部分中,其他任何文件夹中都没有错误。 I have tried everything I could find and saw that it is a bug in Eclipse but sometimes it is the fault of the programmer. 我尝试了所有可能找到的内容,发现这是Eclipse中的错误,但有时是程序员的错。 I'm not sure which this case is for me. 我不确定哪种情况适合我。 "Variable" in this case is for any line with R.id."variable". 在这种情况下,“变量”适用于带有R.id.“变量”的任何行。

package com.example.tipcalculator1;

import java.text.NumberFormat;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.app.Activity;

public class MainActivity extends ActionBarActivity implements
    OnEditorActionListener, OnClickListener {
// define instance variables for the widgets
EditText billAmountEditText;
TextView percentTextView;
Button percentUpButton;
Button percentDownButton;
TextView tipTextView;
TextView totalTextView; 


// define an instance variable for tip percent
float tipPercent = .15f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    billAmountEditText = (EditText) findViewById(R.id.billAmountEditText);
    percentTextView = (TextView) findViewById(R.id.percentTextView);
    percentUpButton = (Button) findViewById(R.id.percentUpButton);
    percentDownButton = (Button) findViewById(R.id.percentDownButton);
    tipTextView = (TextView) findViewById(R.id.tipTextView);
    totalTextView = (TextView) findViewById(R.id.totalTextView);

    // set the listeners
    billAmountEditText.setOnEditorActionListener(this);
    percentUpButton.setOnClickListener(this);
    percentDownButton.setOnClickListener(this);
    // calculate the tip and display the results
    calculateAndDisplay();
}

public void calculateAndDisplay() {
    // get the bill amount
    String billAmountString = billAmountEditText.getText().toString();
    float billAmount;
    if (billAmountString.equals("")) {
        billAmount = 0;
    } else {
        billAmount = Float.parseFloat(billAmountString);
    }

    // calculate tip and total
    float tipAmount = billAmount * tipPercent;
    float totalAmount = billAmount + tipAmount;

    // display the results with formatting
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    tipTextView.setText(currency.format(tipAmount));
    totalTextView.setText(currency.format(totalAmount));
    NumberFormat percent = NumberFormat.getPercentInstance();
    percentTextView.setText(percent.format(tipPercent));
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    calculateAndDisplay();
    return false;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.percentDownButton:
        tipPercent = tipPercent - .01f;
        calculateAndDisplay();
        break;
    case R.id.percentUpButton:
        tipPercent = tipPercent + .01f;
        calculateAndDisplay();
        break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

You may 你可以

  1. Go to Project > Properties > Java Build Path > tab [Order and Export] > Tick Android Version Checkbox, and then Properties again > Build Project . 转到项目>属性> Java构建路径>选项卡[订购和导出]>勾选Android版本复选框,然后再次单击属性>构建项目。

  2. Mistakes in your xml views could cause the R.java not to be generated. xml视图中的错误可能导致未生成R.java。 Go through your view files and make sure all the xml is right 浏览您的查看文件,并确保所有xml正确

Go to your string.xml under res\\values add one line 转到res\\values下的string.xml ,添加一行

<string name="action_settings">Settings</string>

such that: 这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Hello Map</string>
    <string name="action_settings">Settings</string>
</resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM