-1
EditText crr1 = (EditText) findViewById(R.id.ch1);
        add=Double.parseDouble(crr1.getText().toString());
        EditText crr2 = (EditText) findViewById(R.id.ch2);
        add1=(Double.parseDouble(crr2.getText().toString()));
        EditText crr3 = (EditText) findViewById(R.id.ch3);
        add2=(Double.parseDouble(crr3.getText().toString()));
        EditText crr4 = (EditText) findViewById(R.id.ch4);
        add3=(Double.parseDouble(crr4.getText().toString()));
        EditText crr5 = (EditText) findViewById(R.id.ch5);
        add4=(Double.parseDouble(crr5.getText().toString()));
        EditText crr6 = (EditText) findViewById(R.id.ch6);
        add5=(Double.parseDouble(crr6.getText().toString()));
        tadd=add+add1+add2+add3+add4+add5;

I'm adding the values of edittext with double.. why it's showing error? when I call this code, why app close automatically..!!

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • Are you using Eclipse for developing? If so, use Logcat http://developer.android.com/tools/debugging/debugging-log.html it's very useful to analyze errors while executing – SebasSBM May 06 '15 at 19:45
  • Sorry @slanecek new to site and android therefore it's happened. But I found the problem. Thanks all of you.. I should check the empty values and replace those with minimum 0. Thanks – Asif Mushtaq May 06 '15 at 20:09

3 Answers3

1

You haven't posted stacktrace, but ok.

You initialize an Edit Text and then you are trying to parse a double from an EMPTY string that you got from edittext.getText().toString(). That's why it's crashing.

If you run your app in debugging mode on a physical device, you can use Logcat to see the log's stacktrace.

How to enable LogCat/Console in Eclipse for Android?

Community
  • 1
  • 1
slanecek
  • 808
  • 1
  • 8
  • 24
  • Hi thanks for your interest, can you explain how i can solve it? I'm little confused.. there is already value in ch1 id.. then how it could be empty? – Asif Mushtaq May 06 '15 at 19:45
  • 1
    When you call findViewById(), it creates an Edit Text. It is created EMPTY, it has value "". On the next line you try to read a double value from the empty string. Then it crashes, because in the empty string is no double value. Look at Bojan Kseneman answer. – slanecek May 06 '15 at 19:50
  • Hi Thanks I found the problem..!! I should check the empty value.. and replace that with 0..!! Got the point.. Thanks.. – Asif Mushtaq May 06 '15 at 20:07
  • If my answer helped, please, accept it as the solution. – slanecek May 06 '15 at 20:08
0

Your app possibly crashes because the value in EditText is not a number and you get the NumberFormatException. Surround your code with try, catch

try {
      double a = Double.parseDouble(et.getText().toString());
}
catch (NumberFormatException e){
    // Not a number. EditText empty???
}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
0

In your onCreate method:

    EditText crr1 = (EditText) findViewById(R.id.ch1);        
    EditText crr2 = (EditText) findViewById(R.id.ch2);
    EditText crr3 = (EditText) findViewById(R.id.ch3);
    EditText crr4 = (EditText) findViewById(R.id.ch4);
    EditText crr5 = (EditText) findViewById(R.id.ch5);
    EditText crr6 = (EditText) findViewById(R.id.ch6);
    Button btnResult =(Button) findViewById(R.id.btn1);

Now add a button click event:

btnResult.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                  add=Double.parseDouble(crr1.getText().toString());
                  add1=(Double.parseDouble(crr2.getText().toString()));
                  add3=(Double.parseDouble(crr4.getText().toString()));
                  add4=(Double.parseDouble(crr5.getText().toString()));
                  add5=(Double.parseDouble(crr6.getText().toString()));
                  tadd=add+add1+add2+add3+add4+add5;
            }
        });

All the variable should be global.

Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
  • Sorry I'm little confused... add=Double.parseDouble(crr1.getText().toString()); what is difference between yours and my code? – Asif Mushtaq May 06 '15 at 19:49
  • There isn't any difference. I meant to say, this line as well as all the other lines of similar kind must be added inside a button click event (not in onCreate method). – Mohammad Arman May 06 '15 at 19:51
  • You mean I need to work with one by one ? and different id will not work? line are sames but ID's are different.. I'm really sorry may be I'm confused.. -_- – Asif Mushtaq May 06 '15 at 19:55
  • Nope, you are getting confused. I am updating the answer, for better understanding. – Mohammad Arman May 06 '15 at 19:58
  • Okay thanks, It would be better if you can paste an example same as my code. – Asif Mushtaq May 06 '15 at 20:00
  • Hi Thanks I found the problem..!! I should check the empty value.. and replace that with 0..!! Got the point.. Thanks.. – Asif Mushtaq May 06 '15 at 20:07
  • i think after your code I could face same problem, If I will leave any edittext empty then it will crash the app..!! – Asif Mushtaq May 06 '15 at 20:14
  • 1
    Yes thats basic thing! As you are parsing the editText input into double. It will fail to parse a null object into a double variable. You can check if(add != null) then you will do that thing. else add = 0.0 something like that. – Mohammad Arman May 06 '15 at 20:17