String Conversion to Double returning null

Asked

Viewed 128 times

1

I have an android application that I need to capture the entries and store in my Order object, but I need to convert the entries(Strings) into Double numbers, but from the following error: java.lang.Numberformatexception: Empty String. Where am I going wrong?

package com.example.diego.precoeprazocorreios;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class SegundaActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

private TextView vLarguraCX;
private TextView vAlturaCX;
private TextView vComprimentoCX;
private EditText vLarguraCX1 ;
private EditText vAlturaCX1;
private EditText vComprimentoCX1;
private SeekBar vPeso;
private Button btnProximo2;
private TextView kilo;
private TextView vlarguraEnv;
private TextView vComprimentoEnv;
private EditText vLarguraEnv1;
private EditText vComprimentoEnv1;
private TextView vDiametroRolo;
private EditText vDiametroRolo1;
private TextView vComprimentoRolo;
private EditText vComprimentoRolo1;
private RadioGroup RadioGroup1;
private TextView txtDeclaracao;
private EditText valorDeclaracao;
private int progress_value;
Encomenda enc = new Encomenda();

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

    RadioGroup1 = (RadioGroup)findViewById(R.id.rg);
    //Variáveis de entradas para o PACOTE
    vLarguraCX = (TextView)findViewById(R.id.larguraCaixa);
    vLarguraCX1 = (EditText) findViewById(R.id.larguraCaixa1);
    vAlturaCX = (TextView)findViewById(R.id.alturaCaixa);
    vAlturaCX1 = (EditText) findViewById(R.id.alturaCaixa1);
    vComprimentoCX = (TextView)findViewById(R.id.comprCaixa);
    vComprimentoCX1 = (EditText) findViewById(R.id.comprCaixa1);
    //Variáveis de entradas para o ENVELOPE
    vlarguraEnv = (TextView)findViewById(R.id.larguraEnv);
    vLarguraEnv1 = (EditText)findViewById(R.id.larguraEnv1);
    vComprimentoEnv = (TextView)findViewById(R.id.comprEnv);
    vComprimentoEnv1 = (EditText)findViewById(R.id.comprEnv1);
    //Variáveis de entradas para o ROLO
    vDiametroRolo = (TextView)findViewById(R.id.diametroRolo);
    vDiametroRolo1 = (EditText)findViewById(R.id.diametroRolo1);
    vComprimentoRolo = (TextView)findViewById(R.id.comprRolo);
    vComprimentoRolo1 = (EditText)findViewById(R.id.comprRolo1);
    //Checkbox e radioButtons
    txtDeclaracao = (TextView)findViewById(R.id.txtValorDeclarado);
    valorDeclaracao = (EditText)findViewById(R.id.valorDeclarado);
    btnProximo2 = (Button) findViewById(R.id.proximo2);
    final CheckBox chmaoPropria = (CheckBox)findViewById(R.id.maopropria);
    final CheckBox chDeclaracao = (CheckBox)findViewById(R.id.declaracao);
    final CheckBox chAR = (CheckBox)findViewById(R.id.AR);
    RadioGroup1.setOnCheckedChangeListener(this);
    seebar();

    //Verifica se o checkbox Aviso de Recebimento foi clicado
    chAR.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(chAR.isChecked()){
                txtDeclaracao.setVisibility(View.VISIBLE);
                valorDeclaracao.setVisibility(View.VISIBLE);
                enc.setAvisoRecibo(valorDeclaracao.getText().toString());
            }else{
                txtDeclaracao.setVisibility(View.GONE);
                valorDeclaracao.setVisibility(View.GONE);
            }
        }
    });
    //Verifica se o checkbox Mão Própria foi clicado
    chmaoPropria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(chmaoPropria.isChecked()){
                enc.setMaoPropria("S");
            }else {
                enc.setMaoPropria("N");
            }
        }
    });
    //Verifica se o checkbox Declaração de recebimento foi clicado
    chDeclaracao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(chDeclaracao.isChecked()){
                enc.setAvisoRecibo("S");
            }else {
                enc.setAvisoRecibo("N");
            }
        }
    });
}

public void seebar(){

    vPeso = (SeekBar) findViewById(R.id.peso);
    kilo = (TextView)findViewById(R.id.kg);
    kilo.setText(vPeso.getProgress() + "kgs / " + vPeso.getMax() + "kgs");
    vPeso.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progress_value = progress;
            kilo.setText(progress + "kgs / " + vPeso.getMax() + "kgs");
            enc.setPeso(String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            kilo.setText(progress_value + "kgs / " + vPeso.getMax() + "kgs");

        }
    });
}

public void onCheckedChanged(RadioGroup group, int checkedId) {

    //Verifica se o radioButtom Caixa foi clicado
    if(checkedId ==  R.id.caixa){
        //Exibe os campos referentes a caixa
        vLarguraCX.setVisibility(View.VISIBLE);
        vLarguraCX1.setVisibility(View.VISIBLE);
        vAlturaCX.setVisibility(View.VISIBLE);
        vAlturaCX1.setVisibility(View.VISIBLE);
        vComprimentoCX.setVisibility(View.VISIBLE);
        vComprimentoCX1.setVisibility(View.VISIBLE);
        //Oculta os demais campos
        vlarguraEnv.setVisibility(View.GONE);
        vLarguraEnv1.setVisibility(View.GONE);
        vComprimentoEnv.setVisibility(View.GONE);
        vComprimentoEnv1.setVisibility(View.GONE);
        vDiametroRolo.setVisibility(View.GONE);
        vDiametroRolo1.setVisibility(View.GONE);
        vComprimentoRolo.setVisibility(View.GONE);
        vComprimentoRolo1.setVisibility(View.GONE);

        vLarguraCX1.getText().toString();
        vAlturaCX1.getText().toString();
        vComprimentoCX1.getText().toString();
        enc.setFormato(1);
        enc.setLargura(Double.parseDouble(vLarguraCX1.getText().toString()));
        enc.setAltura(Double.parseDouble(vAlturaCX1.getText().toString()));
        enc.setComprimento(Double.parseDouble(vComprimentoCX1.getText().toString()));

        //Verifica se o radioButtom Envelope foi clicado
    }else if(checkedId == R.id.envelope) {
        //Exibe os campos referentes a envelope
        vlarguraEnv.setVisibility(View.VISIBLE);
        vLarguraEnv1.setVisibility(View.VISIBLE);
        vComprimentoEnv.setVisibility(View.VISIBLE);
        vComprimentoEnv1.setVisibility(View.VISIBLE);
        //Oculta os demais campos
        vLarguraCX.setVisibility(View.GONE);
        vLarguraCX1.setVisibility(View.GONE);
        vAlturaCX.setVisibility(View.GONE);
        vAlturaCX1.setVisibility(View.GONE);
        vComprimentoCX.setVisibility(View.GONE);
        vComprimentoCX1.setVisibility(View.GONE);
        vDiametroRolo.setVisibility(View.GONE);
        vDiametroRolo1.setVisibility(View.GONE);
        vComprimentoRolo.setVisibility(View.GONE);
        vComprimentoRolo1.setVisibility(View.GONE);

        enc.setFormato(2);
        enc.setLargura(Double.valueOf(vLarguraEnv1.getText().toString()));
        enc.setComprimento(Double.valueOf(vComprimentoEnv1.getText().toString()));


        //Verifica se o radioButtom Rolo foi clicado
    }else if(checkedId == R.id.rolo) {
        //Exibe os campos referentes a rolo
        vDiametroRolo.setVisibility(View.VISIBLE);
        vDiametroRolo1.setVisibility(View.VISIBLE);
        vComprimentoRolo.setVisibility(View.VISIBLE);
        vComprimentoRolo1.setVisibility(View.VISIBLE);
        //Oculta os demais campos
        vLarguraCX.setVisibility(View.GONE);
        vLarguraCX1.setVisibility(View.GONE);
        vAlturaCX.setVisibility(View.GONE);
        vAlturaCX1.setVisibility(View.GONE);
        vComprimentoCX.setVisibility(View.GONE);
        vComprimentoCX1.setVisibility(View.GONE);
        vlarguraEnv.setVisibility(View.GONE);
        vLarguraEnv1.setVisibility(View.GONE);
        vComprimentoEnv.setVisibility(View.GONE);
        vComprimentoEnv1.setVisibility(View.GONE);

        enc.setFormato(3);
        enc.setDiametro(Double.valueOf(vDiametroRolo1.getText().toString()));
        enc.setComprimento(Double.valueOf(vComprimentoRolo1.getText().toString()));
    }

    btnProximo2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(SegundaActivity.this, TerceiraActivity.class);
            myIntent.putExtra("objeto", enc);
            startActivity(myIntent);
        }
    });
}
}

Exit: inserir a descrição da imagem aqui

  • 1

    It seems the string is coming empty

  • @Andersonhenrique just don’t know the why. I can’t find the error there.

  • He says it’s on the second Activity line 166, can you tell me the name of the string that’s on that line?

1 answer

1


When you invoke the method onCheckedChanged (with changes in the component RadioGroup1) it is probable that the values of vDiametroRolo1 and vComprimentoRolo1 are empty (as stated in the question comment!), so an error occurs in the conversion!

You can correct yourself in some ways:

1. Initializing values with 0.

Example:

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

    vDiametroRolo1 = (EditText)findViewById(R.id.diametroRolo1);
    vDiametroRolo1.setText("0"); // iniciamos com zero!
    vComprimentoRolo1 = (EditText)findViewById(R.id.comprRolo1);
    vComprimentoRolo1.setText("0"); // iniciamos com zero!
   .... 
}

2. Processing in the method onCheckedChanged:

        if(!"".equals(vDiametroRolo1.getText().toString())) {
            vDiametroRolo1.setLargura(Double.valueOf(vLarguraEnv1.getText().toString()));
        }
        if(!"".equals(vComprimentoEnv1.getText().toString())) {
            vDiametroRolo1.setLargura(Double.valueOf(vComprimentoEnv1.getText().toString()));
        }

Which one should you use?

This who will answer is your business rule!

If these values are mandatory, it will be necessary to validate the fields!

  • I believe it’s not quite that, because I switched from double to string and took the values normally. vLarguraCX1.getText().toString();
 vAlturaCX1.getText().toString();
 vComprimentoCX1.getText().toString(); enc.setFormato(1);
 enc.setLargura(vLarguraCX1.getText().toString());
 enc.setAltura(vAlturaCX1.getText().toString());
 enc.setComprimento(vComprimentoCX1.getText().toString());

  • the problem is, I need those entries to be double. But I can’t convert them

  • the fields width, length, input and diameter, just the ones that are double in my order object. I put all these variables as string in the object, and with the code in the above comment it takes the normal values. Now double, does not work

  • yes, only numbers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.