0

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.

javanna
60.4k14 gold badges147 silver badges125 bronze badges
asked Dec 29, 2011 at 14:36
3
  • 1
    What do you receive in Logcat? Commented Dec 29, 2011 at 14:40
  • 1
    Please use Log.i() method and see what happen at Logcat. Commented Dec 29, 2011 at 14:40
  • 2
    Problem is described in detail in your logcat. Usually carefull reading of stacktrace solves 90% of problems Commented Dec 29, 2011 at 14:40

2 Answers 2

3

You have to catch NumberFormatException instead of ArithmeticException.

And initialize solution variable before. Like:

solution = (TextView) findViewById(R.id.solution);

Full code will be:

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);
solution = (TextView) findViewById(R.id.solution);
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 (NumberFormatException e) {
 e.printStackTrace();
 }
 }
 });
 }
 }
answered Dec 29, 2011 at 14:43
Sign up to request clarification or add additional context in comments.

3 Comments

Also, "solution" is not initialized.
@user1121334 Provide the error message you got in your LogCat! Please do not ignore comments that request detailed information from you...
Just catching "Exception" is often the quickest way to figure out a problem like this. Refine it once you get it working.
0

Try this:

 solve.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 // TODO Auto-generated method stub
 String A = vA.getText().toString();
 iA = Integer.ValueOf(A);
 String B = vB.getText().toString();
 iB = Integer.ValueOf(B);
 String C = vC.getText().toString();
 iC = Integer.ValueOf(C);
 // intialize solution
 solution.setText("Yout total is " + String.ValueOf(iA));
 //This is not the total yet
 }
 });
answered Dec 29, 2011 at 14:46

1 Comment

You must tell us what do you receive in LogCat to be able to help you

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.