My application shut down unexpectedly, I don’t really know what the problem is. I’m new to programming java. I tried the ‘try’, ‘catch’ and ‘finally’ methods.
Here is the code:
package com.eqsec.csaba;
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 EqsecActivity extends Activity {
/** Called when the activity is first created. */
Button solve;
EditText vA, vB, vC;
TextView solution;
int discriminant, iA, iB, iC;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
solve = (Button) findViewById(R.id.bSolve);
vA = (EditText) findViewById(R.id.etA);
vB = (EditText) findViewById(R.id.etB);
vC = (EditText) findViewById(R.id.etC);
solve.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try{
String A = vA.getText().toString();
iA = Integer.parseInt(A);
String B = vB.getText().toString();
iB = Integer.parseInt(B);
String C = vC.getText().toString();
iC = Integer.parseInt(C);
solution.setText("Yout total is " + iA);
}catch (ArithmeticException e) {
e.printStackTrace();
}
}
});
}
}
Please help me, i want to write an android application, which can solve some math problems.
You have to catch
NumberFormatExceptioninstead ofArithmeticException.And initialize solution variable before. Like:
Full code will be: