Because when I try to run my application on the telophone it closes?

2

because I am currently using android studio and basically when I add the EditText, Buttons and Textview in the design I do not get any error but when I add code to the Onclick so that this event is executed in a button I close the app on the phone and I would like to know what the error is, they have told me that it is because of my code that it must have some logical error or so, but in reality, no, my code has no errors and I do not know which other error is

public class divisas1 extends AppCompatActivity implements View.OnClickListener {
private Button casa;
    private Button ok;
    private Button clean;
private EditText txt;
private TextView dol;
    private TextView eu;
    private TextView yn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_divisas1);
        casa.findViewById(R.id.menu);
        ok.findViewById(R.id.aceptar);
        clean.findViewById(R.id.borrar);
        txt.findViewById(R.id.texto);
        dol.findViewById(R.id.dolares);
        eu.findViewById(R.id.euros);
        yn.findViewById(R.id.yenes);

        casa.setOnClickListener(this);
        ok.setOnClickListener(this);
        clean.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        try{
            switch (view.getId()){
                case R.id.aceptar:
                    String pl =txt.getText().toString();
                    double pesos=Double.parseDouble(pl);
                    double d,y,e;
                    d=pesos/17;
                    dol.setText(""+d);
                    e=pesos/20;
                    eu.setText(""+e);
                    y=pesos/0.17;
                    yn.setText(""+y);



                    break;
                case R.id.borrar:
                    dol.setText("");
                    eu.setText("");
                    yn.setText("");
                    break;

                case R.id.menu:
                    Intent mmm= new Intent(divisas1.this, MainActivity.class);
                    startActivity(mmm);
                    this.finish();
                    break;




            }




        }catch (Exception ignored){

        }
    }
}

Do you find any error in this code?

    
asked by diiego juarez 16.03.2018 в 22:53
source

1 answer

2

The problem is that the references of the views are being obtained incorrectly, when allocating the listeners to the variables house, ok, clean these have null value:

Button casa, ok, clean;
EditText txt;
TextView dol, ey, yn;

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

        //obten referencias de las vistas dentro de activity_divisas.xml

        casa = (Button)findViewById(R.id.menu);
        ok = (Button)findViewById(R.id.aceptar);
        clean = (Button)findViewById(R.id.borrar);

        txt = (EditText)findViewById(R.id.texto);

        dol = (TextView)findViewById(R.id.dolares);
        eu = (TextView)findViewById(R.id.euros);
        yn = (TextView)findViewById(R.id.yenes);


        //Asigna listener!
        casa.setOnClickListener(this);
        ok.setOnClickListener(this);
        clean.setOnClickListener(this);

    }

It is important to mention that these views must be inside the file activity_divisas.xml and must be of the type you defined in the variable.

For example within the file activity_divisas.xml a button with id menu must be found.

casa = (Button)findViewById(R.id.menu)
    
answered by 16.03.2018 / 23:35
source