0

计算金额时出错,三个字段名称、数量、价格,其中名称和价格从第一个活动和数量编辑文本中获取,允许用户输入数量,但出现错误:-
最终局部变量 qty、价格、总计无法分配因为它是在封闭类型 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));


        }
    });

}
}
4

1 回答 1

0

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

于 2012-09-26T06:13:50.790 回答