Code problem

  • Replies:1
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Jan 11, 2019, 9:21:24 PM via Website

There are no errors in the android studio, the application crashes. What I do wrong. . It happened when I inserted a part of the color change code. Here's the code:

public void buttonClickedIzracunaj(View v) {

    EditText visina = (EditText) findViewById(R.id.txtVisina);
    EditText tezina = (EditText) findViewById(R.id.txtTezina);
    TextView rezultat = (TextView) findViewById(R.id.txtRez);

    if (visina.getText().length() == 0 && tezina.getText().length() == 0) return;

    double vi = Double.parseDouble(visina.getText().toString());
    double te = Double.parseDouble(tezina.getText().toString());

    double ITM = te / ((vi / 100) * (vi / 100));

    rezultat.setText(String.format("%.2f", ITM));

    String value = rezultat.getText().toString();
    int i = Integer.parseInt(value);

    if(i<20){
        rezultat.setTextColor(Color.parseColor("blue"));
    }else if(i<=20 && i<25){
        rezultat.setTextColor(Color.parseColor("green"));
    }else if(i>25){
        rezultat.setTextColor(Color.parseColor("red"));
    }
Reply
  • Forum posts: 922

Jan 12, 2019, 5:33:40 AM via Website

Perhaps there will be an exception in Double.parseDouble() if the value of txtVisina is something like 'abcd'. The exception will result in an app crash.

You had better take advantage of try-catch method in your codes.

— modified on Jan 12, 2019, 6:22:59 AM

Size 0.07 MB? Google Play - https://goo.gl/ws42fN
Google Blog - https://okblackcoffee.blogspot.com

Helpful?
Reply
Recommended articles