【问题标题】:the final local variable qty price total cannot be assigned since it is defined in an enclosing type android最终的局部变量 qty price total 无法赋值,因为它是在封闭类型 android 中定义的
【发布时间】:2012-09-26 06:09:34
【问题描述】:

在计算金额时出错,三个字段名称、数量、价格,其中名称和价格从第一个活动和数量编辑文本中获取,允许用户输入数量,但出现错误:-
最终的局部变量 qty, price, total 不能赋值,因为它是在封闭类型 android 中定义的。请看源码

public class SecondScreenActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen2);

    TextView txtName = (TextView) findViewById(R.id.txtName);
    final TextView txtCost = (TextView) findViewById(R.id.txtCost);
    final EditText txtQty=(EditText)findViewById(R.id.txtQty);
    final double price = Double.parseDouble(txtCost.getText().toString());
    final double qty = Double.parseDouble(txtQty.getText().toString());
    final double total=0;


    Button btnClose = (Button) findViewById(R.id.btnCalculate);
    final TextView txtResult = (TextView) findViewById(R.id.txtResult);

    Intent i = getIntent();
    // Receiving the Data
    String name = i.getStringExtra("name");
    String cost = i.getStringExtra("cost");

    // Displaying Received data
    txtName.setText(name);
    txtCost.setText(cost);

    // Binding Click event to Button
    btnClose.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Closing SecondScreen Activity
            //finish();
        //Getting Error Here
               //the final local variable qty price total 
               //cannot be assigned since it is defined 
               //in an enclosing type android               
        qty=Double.parseDouble(txtQty.getText().toString());
        price=Double.parseDouble(txtCost.getText().toString());
        total=qty*price;
        txtResult.setText(Double.toString(total));


        }
    });

}
}

【问题讨论】:

    标签: android android-intent android-emulator android-widget


    【解决方案1】:

    您不需要在 onCreate() 中定义变量 qty 和 price。仅在您的 OnClickListener 中定义它们,一切都会好起来的。设置最终变量后,您将无法更改它们。这就是为什么它们是最终的。

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 2014-02-24
      • 2016-03-15
      • 2015-02-26
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      相关资源
      最近更新 更多