我正在尝试获取用户输入的数字,并使用一些指定的值对其进行多次输入。我无法看到我犯错误的地方,我的应用程序一直在崩溃。我甚至无法运行它。
MainActivity.java
public abstract class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
Button calculateBtn;
EditText userEcts;
TextView summary;
int price = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userEcts = (EditText) findViewById(R.id.ects_input);
summary = (TextView) findViewById(R.id.text_summary);
calculateBtn = (Button) findViewById(R.id.calculate);
calculateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When the button is clicked, call the calculate method.
calculate();
}
});
}
public void calculate() {
//gets entered text from the EditText,and convert to integers.
Double ects = Double.parseDouble(userEcts.getText().toString());
//do the calculation
Double calculatedPrice = ects * price;
//set the value to the TextView, to display on screen.
summary.setText("Ukupno za platit: " + Double.toString(calculatedPrice) + "kn");
}
}
答案 0 :(得分:0)
您正在实施OnItemSelectedListener
change OnItemSelectedListener to View.OnClickListener
答案 1 :(得分:0)
你应该这样做:
summary.setText("Ukupno za platit: " + calculatedPrice + "kn");
String + Double = String
答案 2 :(得分:0)
我个人建议你在解析一个字符串之前在compute方法上放一个像这样的检查,因为空字符串在解析它时也会导致崩溃:
public void calculate() {
//Put this empty check to prevent crash while parsing empty string.
if (!TextUtils.isEmpty(userEcts.getText.toString())){
Double ects = Double.parseDouble(userEcts.getText().toString());
//do the calculation
Double calculatedPrice = ects * price;
//set the value to the TextView, to display on screen.
summary.setText("Ukupno za platit: " + Double.toString(calculatedPrice) + "kn");
}
}
希望这有帮助。
答案 3 :(得分:0)
现在可以使用...在现有代码中添加了此代码
@Override
public void onClick(View v) {
calculate();
}
非常感谢:)