-1

I'm trying to make an android app, but on launching the main activity on an developer device the logcat goes like this:

The device is Samsung GT-S5282: Rooted: Resurrection Remix (Marshmallow)

    07-23 21:35:35.840 28372-28372/? I/art: Late-enabling -Xcheck:jni
07-23 21:35:36.460 28372-28372/com.win.rd.calculator W/System: ClassLoader referenced unknown path: /data/app/com.win.rd.calculator-1/lib/arm
07-23 21:35:36.550 28372-28382/com.win.rd.calculator I/art: Background sticky concurrent mark sweep GC freed 2992(169KB) AllocSpace objects, 0(0B) LOS objects, 52% free, 615KB/1294KB, paused 5.118ms total 79.262ms
07-23 21:35:36.610 28372-28372/com.win.rd.calculator W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    unsupported multiclient unsolicited response code 1009
07-23 21:35:37.020 28372-28372/com.win.rd.calculator D/AndroidRuntime: Shutting down VM
07-23 21:35:37.030 28372-28372/com.win.rd.calculator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.win.rd.calculator, PID: 28372
    Theme: themes:{}
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.win.rd.calculator/com.win.rd.calculator.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5471)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NumberFormatException: Invalid double: ""
        at java.lang.StringToReal.invalidReal(StringToReal.java:63)
        at java.lang.StringToReal.parseDouble(StringToReal.java:267)
        at java.lang.Double.parseDouble(Double.java:301)
        at java.lang.Double.valueOf(Double.java:338)
        at com.win.rd.calculator.MainActivity.onCreate(MainActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:7125)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5471) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

What's going wrong?

Here is my main activity:

package com.win.rd.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText edt1, edt2;
    TextView txt;
    Button pls, min, mlp, div;

    double numA, numB, result;
    int flag, ecd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edt1 = (EditText) findViewById(R.id.num1);
        edt2 = (EditText) findViewById(R.id.num2);
        txt = (TextView) findViewById(R.id.res);
        pls = (Button) findViewById(R.id.plus);
        min = (Button) findViewById(R.id.minus);
        mlp = (Button) findViewById(R.id.multiply);
        div = (Button) findViewById(R.id.divide);

        numA = numB = result = flag = 0;

        numA = Double.valueOf(edt1.getText().toString());
        numB = Double.valueOf(edt2.getText().toString());

    }

    public void Operation ( double numA, double numB, int flag){
        if (flag == 1) {
            result = (numA + numB);
        } else if (flag == 2) {
            result = (numA - numB);
        } else if (flag == 3) {
            result = (numA * numB);
        } else if (flag == 4) {
            result = numA / numB;
            if (Double.isInfinite(result)) {
                ecd = 1;
                edt1.setText("");
                edt2.setText("");
                Toast.makeText(this, "Cannot Divide By Zero", Toast.LENGTH_SHORT).show();
            }
            else if (Double.isNaN(result)){
                ecd = 2;
                edt1.setText("");
                edt2.setText("");
                Toast.makeText(this, "0/0 Not possible", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void Plus (View view){
        txt.setText("+");
        flag = 1;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }
    public void Minus (View view){
        txt.setText("-");
        flag = 2;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }

    public void Multiply (View view){
        txt.setText("x");
        flag = 3;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }

    public void Divide (View view){
        txt.setText("/");
        flag = 4;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }

    public void Result (View view){
        if (flag == 4) {
            if (ecd == 1)
                txt.setText("Infinite");
            if (ecd == 2)
                txt.setText("NaN");
        }
        else
            txt.setText(String.valueOf(result));
    }
}

Its a simple calculator app.

Double.valueOf() & Double.double.parseDouble() produces the same result right.? What are other typecasting methods in java. Thanks for quick comment.

1 Answers1

0

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NumberFormatException: Invalid double: ""

The error says that you are trying to convert empty string to double which is causing this issue, so you should handle empty strings cases like below

if(edt1.getText().toString() != null && !edt1.getText().toString().isEmpty()){
    numA = Double.valueOf(edt1.getText().toString());
}
Fahadsk
  • 1,099
  • 10
  • 24
  • `edt1.getText().toString() != null` is always "true" so why add it.. Again, `numA = Double.valueOf(edt1.getText().toString()); numB = Double.valueOf(edt2.getText().toString());` this was read in the beginning, so why to read it again under if? – Jovial Joe Jayarson Jul 24 '18 at 10:23
  • It is just to show that you need to handle empty and null strings before parsing it to double type. – Fahadsk Jul 24 '18 at 11:19
  • great! happy coding. – Fahadsk Jul 25 '18 at 06:11
  • Thanks! A tiny discovery: those `...edt1.getText().toString() != null...` codes must not be in `onCreate()` since they are true, the values are not taken in (or read), so I just put them on another function. Anyway thanks! – Jovial Joe Jayarson Jul 26 '18 at 13:45