Code problem

  • Replies:9
  • OpenNot stickiedAnswered
  • Forum posts: 5

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
Best answer
  • Forum posts: 5

Jan 20, 2019, 8:40:42 AM via Website

Well, it's because the text of your TextView/s can not be parsed as a double; this is due to the comma, add this and it should be fine visina.getText().toString().replaceAll(",", ".")

Helpful?
Reply
  • Forum posts: 988

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

Helpful?
Reply
  • Forum posts: 5

Jan 18, 2019, 5:47:54 PM via Website

Greeting.
Android studio has no errors. The application works on the emulator (API 22). The Samsung J3 2016 is launched but when I press the button calculate the application crashes. The message that writes to J3 "has been a mistake".

Helpful?
Reply
  • Forum posts: 5

Jan 19, 2019, 10:49:06 AM via Website

Can you post the stack trace from logcat in Android Studio?

Helpful?
Reply
  • Forum posts: 5

Jan 19, 2019, 1:44:56 PM via Website

I think this is it:
01-19 13:40:45.682 4700-4700/com.example.marin.bmi_indextjelesnemase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.marin.bmi_indextjelesnemase, PID: 4700
java.lang.NumberFormatException: Invalid double: "21,46"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.marin.bmi_indextjelesnemase.MainActivity$1.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:5246)
at android.widget.TextView.performClick(TextView.java:10620)
at android.view.View$PerformClick.run(View.java:21256)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:7007)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Helpful?
Reply
  • Forum posts: 5

Jan 19, 2019, 2:43:29 PM via Website

Not.
This is the calculated data.
The problem appeared when I wrote a piece of code that changed color in TextView.

Helpful?
Reply
Best answer
  • Forum posts: 5

Jan 20, 2019, 8:40:42 AM via Website

Well, it's because the text of your TextView/s can not be parsed as a double; this is due to the comma, add this and it should be fine visina.getText().toString().replaceAll(",", ".")

Helpful?
Reply
  • Forum posts: 18

Apr 8, 2019, 1:24:43 PM via Website

Process: com.example.marin.bmi_indextjelesnemase, PID: 4700
java.lang.NumberFormatException: Invalid double: "21,46"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)

Helpful?
Reply
Recommended articles