Application crashes

  • Replies:3
Deepak Singh
  • Forum posts: 1

Nov 21, 2015, 5:09:12 AM via Website

This is my code below it complies properly and when i click on the calculate button the app crashes i dont understand y?????

package thumbplay.tip;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class tip extends Activity {

double tbill;
double tippercent;
double tip_cal;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tip);
    final EditText billamt = (EditText) findViewById(R.id.tbill);

// final EditText tip=(EditText)findViewById(R.id.tpercent);
final EditText tip_percent = (EditText) findViewById(R.id.tpercent);
final TextView totalbill = (TextView) findViewById(R.id.gt);
//final TextView tipamount=(TextView)findViewById(R.id.tipamount);
//final TextView hpays=(TextView)findViewById(R.id.hpays);
final Button calculate = (Button) findViewById(R.id.caculate);

    calculate.setOnClickListener(new OnClickListener() {
                                     public void onClick(View v) {
                                          tbill = Double.parseDouble(billamt.toString());
                                         tippercent = Double.parseDouble(tip_percent.toString());
                                         tip_cal = (tbill * tippercent) / 100;
                                         totalbill.setText(Double.toString(tip_cal));
                                     }
                                 }
    );
}

}

Reply
James Watson
  • Forum posts: 1,584

Nov 21, 2015, 10:30:38 AM via Website

If the value of either TextBox percent or TextBox billamt is a invalid double, you must get a crash when you click the 'Calculate' button.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
James Watson
  • Forum posts: 1,584

Nov 21, 2015, 10:35:28 AM via Website

You should check data validity before call parseDouble().

Or you should try and catch exception by yourself.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
Balint Farago
  • Forum posts: 19

Dec 6, 2015, 11:30:01 AM via Website

To get the edittext's value, use edittext.getText().toString. In your case e.g.

instead of billamt.toString()
use billamt.getText().toString()
and so on.
And make use of the Log command to find out where you app crashes.

Reply