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
-
1What do you receive in Logcat?Mohamed_AbdAllah– Mohamed_AbdAllah2011年12月29日 14:40:45 +00:00Commented Dec 29, 2011 at 14:40
-
1Please use Log.i() method and see what happen at Logcat.KV Prajapati– KV Prajapati2011年12月29日 14:40:50 +00:00Commented Dec 29, 2011 at 14:40
-
2Problem is described in detail in your logcat. Usually carefull reading of stacktrace solves 90% of problemsKonstantin Pribluda– Konstantin Pribluda2011年12月29日 14:40:59 +00:00Commented Dec 29, 2011 at 14:40
2 Answers 2
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
nostra13
12.4k3 gold badges35 silver badges43 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
njzk2
Also, "solution" is not initialized.
WarrenFaith
@user1121334 Provide the error message you got in your LogCat! Please do not ignore comments that request detailed information from you...
RalphChapin
Just catching "Exception" is often the quickest way to figure out a problem like this. Refine it once you get it working.
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
Mohamed_AbdAllah
5,3203 gold badges31 silver badges47 bronze badges
1 Comment
Mohamed_AbdAllah
You must tell us what do you receive in LogCat to be able to help you
lang-java