Click button works on emulator, but does not work on mobile

Asked

Viewed 297 times

0

I have a button and 2 values. When I am in the emulator, everything works great.. But when I export the app and install it on mobile, the same button doesn’t work.

package br.emanuel.imc;

import java.text.DecimalFormat;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.ads.*;


public class MainActivity extends Activity {

private AdView adView;
Button btCalcular;
EditText etPeso, etAltura, etIdade;
TextView tvResultado, tvCategoria, tvPesoIdeal;//< edit 08/06
double  altura,peso,idade;
double resultado, pesoMin, pesoMax;

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

    btCalcular = (Button) findViewById(R.id.btCalcular);

    etPeso = (EditText) findViewById(R.id.etPeso);
    etAltura = (EditText) findViewById(R.id.etAltura);
    etIdade = (EditText) findViewById(R.id.etIdade);

    tvResultado = (TextView) findViewById(R.id.tvResultado);
    tvCategoria = (TextView) findViewById(R.id.tvCategoria);
    tvPesoIdeal = (TextView) findViewById(R.id.tvPesoIdeal);

    /*
    adView = new AdView(this, AdSize.BANNER, "ca-app-pub-5576365539424575/7733482150");
    LinearLayout layout = (LinearLayout)findViewById(R.id.linear);
    layout.addView(adView);
    adView.loadAd(new AdRequest());
    */

    btCalcular.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                if((etPeso.getText().length()==0) || (etAltura.getText().length()==0) || (etIdade.getText().length()==0)){
                    Alerta("ATENÇÂO", "Existem campos vazios. Verifique!");
                    return;
                }else{
                    peso = Double.parseDouble(etPeso.getText().toString());
                    altura = Double.parseDouble(etAltura.getText().toString());
                    idade = Double.parseDouble(etIdade.getText().toString());

                    resultado = peso /(altura*altura);
                    resultado = converterDoubleDoisDecimais(resultado);

                    pesoMin = 18.5*(altura*altura);
                    pesoMin= converterDoubleDoisDecimais(pesoMin);

                    pesoMax = 24.99*(altura*altura);
                    pesoMax = converterDoubleDoisDecimais(pesoMax);

                    tvResultado.setText("Resultado: "+resultado);
                    tvPesoIdeal.setText("Peso Ideal: Entre "+pesoMin+" Kgs e "+pesoMax+" Kgs.");
                    if (resultado <17){
                        tvCategoria.setText("Categoria: Muito abaixo do peso.");
                        return;
                        }
                    else{
                        if ((resultado>=17) && (resultado<=18.49)){
                            tvCategoria.setText("Categoria: Abaixo do Peso.");
                            return;
                        }
                        else{
                            if ((resultado>18.49)&& (resultado <=24.99)){
                                tvCategoria.setText("Categoria: Peso Ideal.");
                                return;
                            }
                            else{
                                if((resultado>24.99) && (resultado<=29.99)){
                                    tvCategoria.setText("Categoria: Acima do peso.");
                                    return;
                                }
                                else{
                                    if((resultado>29.99) && (resultado<=34.99)){
                                        tvCategoria.setText("Categoria: Obesidade I.");
                                        return;
                                    }
                                    else{
                                        if((resultado>34.99)&&(resultado<39.99)){
                                            tvCategoria.setText("Categoria: Obesidade Severa.");
                                            return;
                                        }
                                        else{
                                            tvCategoria.setText("Categoria: Obesidade Mórbida.");
                                            return;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            finally{
                etPeso.setText("");
                etAltura.setText("");
                etIdade.setText("");
            }

        }
    });

}

public void Alerta(String titulo, String msg){
    AlertDialog.Builder alerta = new AlertDialog.Builder(MainActivity.this);
    alerta.setTitle(titulo);
    alerta.setMessage(msg);
    alerta.setNeutralButton("OK",null);
    alerta.show();
}

public static double converterDoubleDoisDecimais(double valorDouble) {  
    DecimalFormat fr = new DecimalFormat("0.00");        
    String string = fr.format(valorDouble);  
    String[] part = string.split("[.]");  
    String string2 = part[0]+"."+part[1];  
    double valor = Double.parseDouble(string2);  
    return valor;  
}  
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

I think I should use the .setOnTouchListener in place of .setOnClickListener. That would be the solution?

  • 2

    Could post the stacktrace error printed on logcat? Helps to know which error and on which line the error occurred.

  • 1

    I believe some error is occurring that is being silenced by catch (Exception e) { // TODO: handle exception }, of a e.printStackTrace(); within catch and capture(and post in question) the error that is occurring so we can help you.

  • @Wakim - Thank you very much for your attention and willingness to help. The problem was in converting to decimal. Specifically on the line: String[] part = string.split("[.]"); ... I did not notice the decimal separator which is comma.

  • @Fernando Idem the comment above.

  • @emanuelsn instead of editing your question with the answer, post an answer with the solution, and you can accept it yourself. This will make it easier to view the problem and the solution for other site visitors.

1 answer

0


Solved

The problem was time to convert to 2 decimal places. You must change the line

String[] part = string.split("[.]"); 

from the convert method:

String[] part = string.split("[,]"); 

Browser other questions tagged

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