从内部类访问变量,需要声明final

时间:2017-11-22 09:44:14

标签: java android

我是android的新手并准备addition project。我想获得value1value2并计算结果。但我收到Variable accessed from inner class, need to declare final的错误。如果我将变量声明为final,那么我会收到cannot assign a value to final variable的另一条错误消息。

package com.example.borntoflirt.design1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AdditionActivity extends AppCompatActivity {

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

    final EditText valueOne;
    final EditText valueTwo;
    final TextView result;
    Button btnAdd;

    final double num1;
    double num2;
    double sum;

    valueOne = (EditText) findViewById(R.id.valueOne);
    valueTwo = (EditText) findViewById(R.id.valueTwo);
    result = (TextView) findViewById(R.id.result);
    btnAdd = (Button) findViewById(R.id.buttonAdd);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num1 = Double.parseDouble(valueOne.getText().toString());
            // result.setText(Double.toString(Double.parseDouble(valueOne.getText().toString()) + Double.parseDouble(valueTwo.getText().toString())));

        }
    });
}
}

2 个答案:

答案 0 :(得分:1)

您的代码应如下所示:

package com.example.borntoflirt.design1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AdditionActivity extends AppCompatActivity {

    private EditText valueOne;
    private EditText valueTwo;
    private TextView result;
    private Button btnAdd;

    private double num1;
    private double num2;
    private double sum;

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

    valueOne = (EditText) findViewById(R.id.valueOne);
    valueTwo = (EditText) findViewById(R.id.valueTwo);
    result = (TextView) findViewById(R.id.result);
    btnAdd = (Button) findViewById(R.id.buttonAdd);

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num1 = Double.parseDouble(valueOne.getText().toString());
            // result.setText(Double.toString(Double.parseDouble(valueOne.getText().toString()) + Double.parseDouble(valueTwo.getText().toString())));

        }
    });
  }
}

如果valueOne为空,则应添加一个部分以检查EditText是否具有某个值,否则可能会出现另一个错误:

public void onClick(View v) {
        if(!valueOne.getText().toString().equals("")) {
            num1 = Double.parseDouble(valueOne.getText().toString());
            // result.setText(Double.toString(Double.parseDouble(valueOne.getText().toString()) + Double.parseDouble(valueTwo.getText().toString())));
        } else {
              Toast.makeText(AdditionActivity.this, "Please add some nunber to continue", Toast.LENGTH_LONG).show();
        }

}

答案 1 :(得分:0)

只需在函数外部的类的根中声明变量即可。

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AdditionActivity extends AppCompatActivity {

    // Just declare the varibles here

    double num1;
    double num2;
    double sum;

    EditText valueOne;
    EditText valueTwo;
    TextView result;
    Button btnAdd;

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

        valueOne = (EditText) findViewById(R.id.valueOne);
        valueTwo = (EditText) findViewById(R.id.valueTwo);
        result = (TextView) findViewById(R.id.result);
        btnAdd = (Button) findViewById(R.id.buttonAdd);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num1 = Double.parseDouble(valueOne.getText().toString());
                // result.setText(Double.toString(Double.parseDouble(valueOne.getText().toString()) + Double.parseDouble(valueTwo.getText().toString())));

            }
        });
    }
}