0

私は本当に NOOB で、新しいコーダーです。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));
}

}

4

3 に答える 3

1

logcat がない場合は、経験に基づいた推測にすぎませんが、次の行を見てください。

etod = (RadioButton)this.findViewById(R.id.euros);

正しい ID を使用していますか? 上記で使用しているものと同じですTextViewClassCastExceptionとして使用しようとしているため、おそらく を投げていますRadioButton

無関係ですが、変換ロジックも間違っているようです。両方の変換が になるとは期待できません(val * 0.67)

于 2013-01-17T01:39:08.380 に答える
0

Window > Show View > Console に移動することをお勧めします。エラーによっては、問題を引き起こしているクラスの行へのリンクが表示されることがあります。

于 2013-01-17T02:47:46.990 に答える
0

logcatなしで推測するのはかなり難しいです。しかし、NPEがスローされていると思います。多分そこから

dollars.getText().toString()

euros.getText().toString()

apply .toString() の前に null チェックを含めることをお勧めします。

于 2013-01-17T01:43:52.640 に答える