我正在尝试使用距离公式,当我尝试运行程序时,我可以为x1,y1,x2输入值,但是当我尝试找到距离时没有结果显示。有什么建议么?距离公式与if语句
公共类MainActivity扩展AppCompatActivity {
EditText editText, editText2, editText3, editText4;
Button button;
TextView tv_result;
double a,b,c,d, e, x1,x2,y1,y2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
button = (Button) findViewById(R.id.button);
//tv_result = (TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!editText.getText().toString().equals("") && !editText2.getText().toString().equals("")
&& !editText3.getText().toString().equals("") && !editText4.getText().toString().equals("")){
x1 = Double.parseDouble(editText.getText().toString());
x2 = Double.parseDouble(editText.getText().toString());
y1 = Double.parseDouble(editText.getText().toString());
y2 = Double.parseDouble(editText.getText().toString());
e = Math.sqrt(Math.pow(x2-x1,2)+ Math.pow(y2-y1,2));
// if (e == 0) {
// tv_result.setText("The two points are the same");
if (e > 0){
tv_result.setText("e");
}
}
}
});
}
}
'// tv_result =(TextView的)findViewById(R.id.tv_result);' - 这是真的线注释掉?这意味着'NullPointerException'。另外,将'tv_result.setText(“e”);'改为'tv_result.setText(Double.toString(e));' – Eran
我认为您需要重新修改问题,因为缺少清晰度@Akshay –