If anyone can help me I would be grateful! It should be converted the three EditText variables into strings and then integers. I added the exception because without it, the program crashed on startup. I’m not sure whether the problem is in the conversion of the variables, in my try catch code, or in both. Please help!
package boston.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TheBostonProjectActivity extends Activity {
public EditText aed, bed, ced;
public TextView dtv;
public int a, b, c;
public Button solve;
public double dis;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aed = (EditText)(findViewById(R.id.etA));
try {
a = Integer.parseInt(aed.getText().toString());
} catch (NumberFormatException e) {
a = 0;
}
bed = (EditText)(findViewById(R.id.etB));
try {
b = Integer.parseInt(bed.getText().toString());
} catch (NumberFormatException e) {
b = 0;
}
ced = (EditText)(findViewById(R.id.etC));
try {
c = Integer.parseInt(ced.getText().toString());
} catch (NumberFormatException e) {
c = 0;
}
solve = (Button)(findViewById(R.id.bsolve));
solve.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
dis = (b*b)-(4*a*c);
dtv = (TextView)findViewById(R.id.tvdis);
dtv.setText("Discriminant:" + dis);
}
});
}
}
You are trying to get the text from EditTexts just after you created them (by calling setContentView). They are all empty – contain no text. And since
Throws an exception, all your catch blocks are executed (and that means, that they actually work, not the contrary).