I’m making a calculator in android, but for some reason it won’t let me read from the edit text. this is the code i used to declare the edit text:
final EditText AnswerBox = (EditText) findViewById(R.id.AnswerBox);
This is the code for one of the buttons:
one.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
AnswerBox.append("1");
}
});
}
This is the method that reads the number:
public Double number_reader()
{
Double num1;
String s;
s=AnswerBox.getText().toString();
num1=Double.valueOf(s);
return num1;
}
The issue is with the AnswerBox in the number_reader method, it says it can’t be resolved..
You probably declare
AnswerBoxin theonCreatemethod:and now
AnswerBoxis a local variable and it doesn’t exist outside theonCreatemethod so it can’t be found in the methodnumber_reader.To resolve it make a private field in your class :
and then in the
onCreatemethod do this: