没有使用局部变量的值?

时间:2015-04-07 15:37:18

标签: java android eclipse

我是Android应用程序开发的新手,所以这对您来说可能很容易。我在发布之前已经搜索了这个论坛,所以请在将其标记为重复之前先查看一下。我没有在updatestandard方法中声明静态变量。这是我的计划:

    package com.example.new1;

   import android.os.Bundle;
   import android.support.v7.app.ActionBarActivity;
   import android.text.TextWatcher;
    import android.text.Editable;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.EditText;
    import android.widget.TextView;
   import android.widget.SeekBar;
     import android.widget.SeekBar.OnSeekBarChangeListener;
         public class MainActivity extends ActionBarActivity {
   private static final String BILL_TOTAL = "BILL_TOTAL";
   private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT";
   private double currentBillTotal; 
   private int currentCustomPercent;
   private EditText tip10EditText;
   private EditText total10EditText;
   private EditText tip15EditText; 
   private EditText total15EditText;
   private EditText billEditText; 
   private EditText tip20EditText; 
   private EditText total20EditText;
   private TextView customTipTextView;
   private EditText tipCustomEditText; 
   private EditText totalCustomEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if( savedInstanceState ==null)
    {
        currentBillTotal=0.0;
        currentCustomPercent=0;

    }
    else
    {
        currentBillTotal=savedInstanceState.getDouble(BILL_TOTAL);
        currentCustomPercent=savedInstanceState.getInt(CUSTOM_PERCENT);
    }
    tip10EditText=(EditText) findViewById(R.id.tip10editText);
    tip15EditText=(EditText) findViewById(R.id.tip15editText);
    tip20EditText=(EditText) findViewById(R.id.tip20editText);
    total10EditText=(EditText) findViewById(R.id.totaltenEditText);
    total20EditText=(EditText) findViewById(R.id.totaltwentyEditText);
    total15EditText=(EditText) findViewById(R.id.totalfifteenEditText);
    tipCustomEditText=(EditText) findViewById(R.id.tipcustomeditText);
    totalCustomEditText=(EditText) findViewById(R.id.totaltcustomeditText);
    billEditText=(EditText) findViewById(R.id.billeditText1);
    billEditText.addTextChangedListener(billEditTextWatcher);
    SeekBar customSeekBar = (SeekBar) findViewById(R.id.customSeekBar);
    customSeekBar.setOnSeekBarChangeListener(customSeekBarListner);
    customTipTextView = (TextView) findViewById(R.id.customTipTextView);    
}
private void updateStandard()
{
    double tenPercentTip= currentBillTotal * .1;
    double tenPercentTotal = currentBillTotal+tenPercentTip;
    total10EditText.setText(String.format("%.02f,tenPercentTotal"));
    double fifteenPercentTip= currentBillTotal * .15;
    double fifteenPercentTotal= currentBillTotal+fifteenPercentTip;
    double twentyPercentTip= currentBillTotal * .20;
    double twentyPercentTotal= currentBillTotal+twentyPercentTip;
    tip10EditText.setText(String.format("%.02f",tenPercentTip));

    tip15EditText.setText(String.format("%.02f",fifteenPercentTip));
    total15EditText.setText(String.format("%.02f,fifteenPercentTotal"));
    tip20EditText.setText(String.format("%.02f",twentyPercentTip));
    total20EditText.setText(String.format("%.02f,twentyPercentTotal"));
}
private void updateCustom()
{
    customTipTextView.setText(currentCustomPercent+"%");
    double customTipAmount=currentBillTotal*currentCustomPercent*.01;
    tipCustomEditText.setText(String.format("%.02f",customTipAmount));
    double customTotalAmount=currentBillTotal+customTipAmount;
    totalCustomEditText.setText(String.format("%.02f",customTotalAmount));
}
@Override
   protected void onSaveInstanceState(Bundle outState)
   {
      super.onSaveInstanceState(outState);

      outState.putDouble(BILL_TOTAL, currentBillTotal);
      outState.putInt(CUSTOM_PERCENT, currentCustomPercent);
   } 
private OnSeekBarChangeListener customSeekBarListner=new OnSeekBarChangeListener()
{

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        currentCustomPercent=seekBar.getProgress();
        updateCustom();
        // TODO Auto-generated method stub1

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

};
private TextWatcher billEditTextWatcher=new TextWatcher()
{

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        try
        {
            currentBillTotal=Double.parseDouble(s.toString());
        }
        catch(NumberFormatException e)
        {
            currentBillTotal=0.0;
        }
        updateStandard();
        updateCustom();
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

};
@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);
}

}

所以在eclipse中它在更新标准行中说变量没有使用tenPercentTotal,十五个PercentTotal和二十个PercentTotal idk为什么它说它们在下面的下一行中使用它请帮助这个确切的程序在我点击右键之前工作项目和选择导出Android应用程序选项搞砸了很多事情,发生了什么事。

2 个答案:

答案 0 :(得分:0)

你在格式化字符串中拼写他们的名字,而不是在String#format()方法调用中实际使用它们作为参数。

答案 1 :(得分:0)

Eclipse没有错(可能刷新项目+验证,如果他已过时)。

将值赋值给变量并不算作用法。