简体   繁体   English

最终的局部变量不能分配,不能分配给非最终变量

[英]The final local variable cannot be assigned , cannot to the non-final variable

I want to write the bmi calculation code in this program. 我想在这个程序中编写bmi计算代码。 When I specify variables "qad1" "vazn1" "bmi" "txtvazn" "txtqad" and "txtbmi" as final , I encountered this error: "The final local variable cannot be assigned" And when I did not specify them as final , I encountered this error: Cannot refer to the non-final local variable defined in an enclosing scope._ change modifier to final. 当我指定变量“qad1”“vazn1”“bmi”“txtvazn”“txtqad”和“txtbmi”作为final ,我遇到了这个错误:“无法分配最终的局部变量”当我没有将它们指定为final ,我遇到了这个错误:无法引用封闭范围中定义的非最终局部变量。将更改修改为final。 I could not solve this problem with any trick.Please help me. 我无法用任何技巧解决这个问题。请帮帮我。 The photo of the codes are uploaded here.Thank you. 代码照片上传到这里。谢谢。

public class Shakhes extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shakhes);

         final EditText txtvazn=(EditText) findViewById(R.id.txtvazn);
         final EditText txtqad=(EditText) findViewById(R.id.txtqad);
         final TextView txtbmi=(TextView) findViewById(R.id.txtbmi);
         Button btnbmi=(Button) findViewById(R.id.btnbmi);
         final int qad1;
         final int vazn1;
         final float bmi;

        btnbmi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                qad1=Integer.parseInt(txtqad.getText().toString());
                qad1=qad1/100;
                vazn1=Integer.parseInt(txtvazn.getText().toString());
                bmi=vazn1/(qad1*qad1);
                txtbmi.setText(""+bmi);
            }
        });
    }
}

You can define variables inside onClickListener if use use them only inside listener: 您可以在onClickListener定义变量,如果只在侦听器中使用它们:

( UPDATE as @DEADMC noted, final is not required more in that case) 更新为@DEADMC注明,在这种情况下, final不需要更多)

 btnbmi.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            int qad1;
            int vazn1;
            float bmi;

            // all other code

On the other hand, if scope of variable is more than listener, convert it to single-item array: 另一方面,如果变量的范围大于侦听器,则将其转换为单项数组:

final int[] qad1 = new int[] {0};

 btnbmi.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
             qad1[0] = ...
       }
 }

Please take some read of immutability benefits in the first place (guess link http://okyasoft.blogspot.com/2014/05/6-benefits-of-programming-with_5146.html ) 请首先阅读一些不可变形性的好处(猜测链接http://okyasoft.blogspot.com/2014/05/6-benefits-of-programming-with_5146.html

For your example, please try to make all the variable references you are trying to use under OnClickListener - as class fields (instead of method variables). 对于您的示例,请尝试在OnClickListener下创建您尝试使用的所有变量引用 - 作为类字段(而不是方法变量)。

Defining, for example, txtbmi as class member ( private TextView txtbmi; ) will automatically make it accessible to all the inner classes of your Shakhes activity. 例如,将txtbmi定义为类成员( private TextView txtbmi; )将自动使其可供Shakhes活动的所有内部类访问。

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

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