我是一个真正的菜鸟和新编码员。我开始学习 Java,最近在 eclipse 上创建了这个应用程序。它是美元到欧元的转换器。每次我尝试在 Eclipse 上运行代码时,都会收到错误消息,指出应用程序已停止工作。
TextView dollars;
TextView euros;
RadioButton dtoe;
RadioButton etod;
Button calculate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dollars = (TextView)this.findViewById(R.id.dollars);
euros = (TextView)this.findViewById(R.id.euros);
dtoe = (RadioButton)this.findViewById(R.id.dtoe);
etod = (RadioButton)this.findViewById(R.id.euros);
calculate = (Button)this.findViewById(R.id.calculate);
calculate.setOnClickListener(this);
}
public void onClick(View v) {
if (dtoe.isChecked()){
convertDollarsToEuros();
}
if (etod.isChecked()){
convertEurosToDollars();
}
}
protected void convertDollarsToEuros(){
double val = Double.parseDouble(dollars.getText().toString());
euros.setText(Double.toString(val*0.67));
}
protected void convertEurosToDollars(){
double val = Double.parseDouble(euros.getText().toString());
dollars.setText(Double.toString(val*0.67));
}
}
最佳答案
如果没有 logcat,这只是一个有根据的猜测,但是看看这一行:
etod = (RadioButton)this.findViewById(R.id.euros);
您确定使用了正确的 ID 吗?它与您在上面的 TextView
中使用的相同。您可能会抛出 ClassCastException
,因为您尝试将其用作 RadioButton
。
不相关,但你的转换逻辑似乎也是错误的。您不能期望两个转换都是(val * 0.67)
。
关于java - 不幸的是,您的<项目名称>已停止工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14370809/