2016-10-02 57 views
-3

我正在尝试使用距离公式,当我尝试运行程序时,我可以为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"); 


       } 






      } 


     } 
    }); 
} 

}

+0

'// tv_result =(TextView的)findViewById(R.id.tv_result);' - 这是真的线注释掉?这意味着'NullPointerException'。另外,将'tv_result.setText(“e”);'改为'tv_result.setText(Double.toString(e));' – Eran

+0

我认为您需要重新修改问题,因为缺少清晰度@Akshay –

回答

-1

更改/ UN注释此行

tv_result = (TextView) findViewById(R.id.tv_result); 

你必须得到一个错误,当您试图绑定一个NULL数据目的! 干杯!

+0

您错过了实际问题这里! –

2

基本上分叉你的代码。尽管做了一些改变。

变化

  1. 取消注释tv_result = (TextView) findViewById(R.id.tv_result);作为@ibtehaz指出,因为它会在tv_result.setText("e");导致NPE。

  2. 在这一行tv_result.setText("e");您将字符“e”设置为TextView而不是您的结果。你应该做tv_result.setText(e);

见下文完整代码,

public class MainActivity extends 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"); 
         } else if (e > 0){ 
          tv_result.setText(String.valueOf(e)); 
         } 
       } 
      } 
     }); 
    } 
} 
+0

哈哈哈。基本上改变了一行:p –

+0

我假设如果回答评论行,他在未注释时尝试。 – Divers

+0

其实更容易。 3行改变了。 :) –