App hangs after clicking button

Asked

Viewed 36 times

1

I have to perform validation of the fields and create the calculation method to tell which is the best fuel, however, when pressing the button, the application hangs. I can’t identify where the mistake is.

Follows code below.

package cursoandroidadivinha.com.myapplication;

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;

public class MainActivity extends AppCompatActivity {

    private EditText editPrecoEtanol;
    private EditText editPrecoGasolina;
    private EditText editConsumoGasolina;
    private EditText editConsumoEtanol;
    private TextView textResultado;
    private Button calcular;

    public MainActivity() {
    }


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

        editPrecoGasolina = findViewById(R.id.editPrecoGasolina);
        editPrecoEtanol = findViewById(R.id.editPrecoEtanol);
        editConsumoGasolina = findViewById(R.id.editConsumoGasolina);
        editConsumoEtanol = findViewById(R.id.editConsumoEtanol);
        calcular = findViewById(R.id.calcular);


        calcular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (editPrecoGasolina.length() == 0) {

                    editPrecoGasolina.setError("Digite o Preço da Gasolina");
                } else if (editPrecoEtanol.length() == 0) {
                    editPrecoEtanol.setError("Digite o Preço do Etanol");
                } else if (editConsumoGasolina.length() == 0) {
                    editConsumoGasolina.setError("Digite o Consumo da Gasolina");
                } else if (editConsumoEtanol.length() == 0) {
                    editConsumoEtanol.setError("Digite o Consumo do Etanol");
                }

                /*double pEtanol      = Double.parseDouble(editPrecoEtanol.getText().toString());
                double pGasolina    = Double.parseDouble(editPrecoGasolina.getText().toString());
                double cEtanol      = Double.parseDouble(editConsumoEtanol.getText().toString());
                double cGasolina    = Double.parseDouble(editConsumoGasolina.getText().toString());

                double resultado = pEtanol * cEtanol / pGasolina * cGasolina;

                if (resultado >= 0.7){
                    textResultado.setText("Melhor Utilizar Gasolina");
                }else {
                    textResultado.setText("Melhor utilizar Etanol ");
            }*/    
            }    
        });
        }    
    }
  • I commented a part of the code to see that the error was in performing the calculations

  • Publish the layout that defines the elements used in this code.

  • 1

    Augusto, thank you so much I already managed to run the code.

No answers

Browser other questions tagged

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