mashhood mashhood - 4 months ago 9
Android Question

variable accessed from with in inner class

When i access my variable within

OnClickListener
it showing me an error

Here is a code :

EditText hight=(EditText)findViewById(R.id.hight);
EditText wight=(EditText)findViewById(R.id.wight);
Button calc=(Button) findViewById(R.id.calculate);
TextView result=(TextView) findViewById(R.id.result);

calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Double h=Double.valueOf(hight.getText().toString());
Double w=Double.valueOf(wight.getText().toString());

h=h/100;


Any help would be appreciated.

Answer

Try this:

 EditText hight,wight;
    Button calc;
    TextView result;


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

        hight=(EditText)findViewById(R.id.hight);
        wight=(EditText)findViewById(R.id.wight);
        calc=(Button) findViewById(R.id.calculate);
        result=(TextView) findViewById(R.id.result);

        calc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Double h=Double.valueOf(hight.getText().toString());
                Double w=Double.valueOf(wight.getText().toString());

                h=h/100;