Reputation: 9
i am a really NOOB and new coder. I started learning Java and recently created this application on eclipse. It is a dollar to euros converter. Everytime i try to run the code on eclipse i get error saying the application has stopped working.
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));
}
}
Upvotes: 0
Views: 154
Reputation: 17
I would suggest going to Window > Show View > Console, depending on the error, it will sometimes give you links to the lines in the classes that are causing problems.
Upvotes: 0
Reputation: 22342
Without the logcat it's only an educated guess, but look at this line:
etod = (RadioButton)this.findViewById(R.id.euros);
Are you sure you're using the right id? It's the same as the one you use for the TextView
above. You're probably throwing a ClassCastException
, because you're trying to use it as a RadioButton
.
Unrelated, but your conversion logic also seems wrong. You can't expect both of the conversions to be (val * 0.67)
.
Upvotes: 1
Reputation: 276
It's pretty hard to guess without a logcat. But I assume there's an NPE being thrown. Most likely it's from
dollars.getText().toString()
and
euros.getText().toString()
I suggest you to include a null check before apply .toString().
Upvotes: 0