嘿傢伙,我真的很想計算不僅c =斜邊,而且還有3或3個EditText-Views中給出的a或b。但它崩潰,不會輸出結果。有什麼建議麼?我對這個東西很新。 :)在android應用程序中的畢達哥拉斯
package net.starostka.somefunctions;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Pythagoras extends AppCompatActivity implements View.OnClickListener {
EditText aNumPyt;
EditText bNumPyt;
EditText cNumPyt;
Button pythagorasCalcu;
TextView pythagorasResultFinal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pythagoras);
aNumPyt = (EditText) findViewById(R.id.aNumPyt);
bNumPyt = (EditText) findViewById(R.id.bNumPyt);
pythagorasCalcu = (Button) findViewById(R.id.pythagorasCalcu);
pythagorasResultFinal = (TextView) findViewById(R.id.pythagorasResultFinal);
// set a listener
pythagorasCalcu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
double a=0,b=0,c=0;
double hypot = 0.0;
/*
// check if the fields are empty
if (TextUtils.isEmpty(aNumPyt.getText().toString()) || TextUtils.isEmpty(bNumPyt.getText().toString()) || TextUtils.isEmpty(cNumPyt.getText().toString())) {
return;
}
*/
// read EditText and fill variables with numbers
a = Double.parseDouble(aNumPyt.getText().toString());
b = Double.parseDouble(bNumPyt.getText().toString());
c = Double.parseDouble(cNumPyt.getText().toString());
if (a>b && a>c){
hypot=a;
if(hypot*hypot==(b*b+c*c)){
//result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
pythagorasResultFinal.setText(String.valueOf(hypot));
}
} else if (b>a && b>c){
hypot=b;
if(hypot*hypot==(a*a+c*c)){
//result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
pythagorasResultFinal.setText(String.valueOf(hypot));
}
} else if (c>a && c>b){
hypot=c;
if(hypot*hypot==(a*a+b*b)){
//result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
pythagorasResultFinal.setText(String.valueOf(hypot));
}
}
}
}
XML文件
</LinearLayout>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:textSize="12pt"
android:layout_marginTop="3pt"
android:id="@+id/pythagorasResultFinal"
android:gravity="center_horizontal">
</TextView>
崩潰之前:
崩潰後:
使用LogCat檢查與您的崩潰相關的Java堆棧跟蹤:https://stackoverflow.com/questions/23353173/unwanted-myapp-has-stopped-how-can-i-solve-this – CommonsWare
也發佈logcat。 –