由于它是在封闭式android中定义的,因此无法分配最终局部变量qty price total

时间:2012-09-26 06:09:34

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

计算金额时获取错误,三个字段名称,数量,价格从第一个活动获得的名称和价格以及qty edittext通过此允许用户输入数量,但收到错误: -
最后的局部变量qty,price,total无法分配,因为它是在封闭式android.please中定义的,请参阅源代码

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


        }
    });

}
}

1 个答案:

答案 0 :(得分:0)

您无需在onCreate()中定义变量数量和价格。只在OnClickListener中定义它们,一切都会好的。设置后无法更改最终变量。这就是为什么他们是最终的。