java - 不幸的是,您的<项目名称>已停止工作。

标签 java android

我是一个真正的菜鸟和新编码员。我开始学习 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/

相关文章:

多线程情况下的Java设计模式: Factory vs Singleton?

java - 如何修复 HackerRank 中的 '<identifier> expected' 错误

android - Kotlin 过滤器在 RecyclerView 中搜索 EditText

java - 格式化存储为字符串的数字

java - 是否可以在 Google App Engine 环境上进行相互 SSL 身份验证?

android - 如何在android中检测长按音量按钮

android - 当另一个应用程序在 Android 中使用它时无法访问麦克风

android - 来自 GIT 的闹钟 - 给出错误 - Android

java - java中if循环内的返回类型

java - CXF客户端: How to ignore @WebServiceClient annotation