繁体   English   中英

不使用局部变量的值?

[英]value of local variable not used?

我是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,fifteenPercentTotal和二十百分之几idk,为什么它说在下面的下一行中使用了变量,请帮助此精确程序在我右键单击项目并选择之前起作用导出android应用程序选项,该选项使idk发生了很多事情。

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

Eclipse没错(如果过时,可以刷新项目+验证)。

将值分配给变量不算作用法。

暂无
暂无

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

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