hi to all i have android application which is calculator that perform only one operation (addition) the result is always 3.0 whatever the two numbers are
what is the solution???
here is the code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class claculator extends Activity {
/** Called when the activity is first created. */
EditText nu1,nu2;
Button add;
TextView txtv;
double x,y,z;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nu1=(EditText)findViewById(R.id.n1);
nu2=(EditText)findViewById(R.id.n2);
add=(Button)findViewById(R.id.btn);
txtv=(TextView)findViewById(R.id.tv);
x= Double.parseDouble(nu1.getText().toString());
y= Double.parseDouble(nu2.getText().toString());
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
z=x+y;
txtv.setText(String.valueOf(z));
}
});
}
}
You must fetch the EditText values inside
onClick():When you try to get these values in
onCreate()the user hasn’t had a chance to alter them yet, you will get their default values. You must fetch these values when the user clicks the Button.