user1985635
user1985635

Reputation: 9

Unfortunately your <Project name> has stopped working.

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

Answers (3)

user1631686
user1631686

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

Geobits
Geobits

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

Rashidi Zin
Rashidi Zin

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

Related Questions