Skip to content
Advertisement

Edit Text to long variable issue?

I am working on a timer app, I have an EditText for taking an input number and based on my radio button selected, multiply the input to be equal to long millisInFuture for the CountDownTimer class. but apparently there is something wrong with parsing my EditText as a long. here is the part for RadioGroup:

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (checkedId == rdSeconds.getId()) {
                et_num.setHint("Seconds");
                String value = et_num.getText().toString();
                //selectedOption is a long variable
                selectedOption = Long.parseLong(value) * 1000;
            } else {
                et_num.setHint("Minutes");
                String value = et_num.getText().toString(); 
                selectedOption = Long.parseLong(value) * 60000; //line 102
            }


        }
    });

Here is part of the error it gaves me?

java.lang.NumberFormatException: For input string: ""
    at java.lang.Long.parseLong(Long.java:455)
    at java.lang.Long.parseLong(Long.java:485)
    at com.example.timemachine.MainActivity$2.onCheckedChanged(MainActivity.java:102)

Advertisement

Answer

check your editText have proper value before parse long

            String value = et_num.getText().toString();

             if(value != null && !value.isEmpty())
             { 
             selectedOption = Long.parseLong(value) * 1000;
             }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement