Error does not appear in login page and program closes when run in emulator

Asked

Viewed 35 times

-1

I am making a login page that if the data is correct or incorrect appears a Toast displaying the result.

The problem is that there is no error (when there is any) and when run in the emulator the application stops working.

Code:

package com.example.appteste;

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.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private login mLogin = new login();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.mLogin.nome = findViewById(R.id.nome);
        this.mLogin.senha = findViewById(R.id.senha);
        this.mLogin.button.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        if ("Arthur" == this.mLogin.nome.getText().toString() & "123" == this.mLogin.senha.getText().toString()) {
            Toast.makeText(this, "Usuario e senha correto!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Usuario ou senha incorretos!", Toast.LENGTH_LONG).show();
        }
    }
    private static class login {
        EditText nome;
        EditText senha;
        Button button;
    }
}
  • You have to show the stack error, so you have the error cause, try to convert the parameters to: this.mLogin.name = (Edittext) findViewById(R.id.name);

  • Emulator error: http://prntscr.com/o9r9vz Console error: put in post

  • I did what you said and gave the same problem.

  • Just remove this.mLogin.button.setOnClickListener(this); The app opens no more I can’t run the event without that line.

  • Add this line node method onCreate this.mLogin.button = (Button) findViewById(R.Id.button); button must be the name of the variable that is your XML node.

1 answer

0


I managed to put a Textview in place of Toast.

Code:

public class MainActivity extends AppCompatActivity {

private login mLogin = new login();

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

    this.mLogin.nome = (EditText) findViewById(R.id.nome);
    this.mLogin.senha = (EditText) findViewById(R.id.senha);
    final TextView resultado = (TextView) findViewById(R.id.resultado);
    Button botao = (Button) findViewById(R.id.button);
    botao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if ("Arthur" == mLogin.nome.getText().toString() & "123" == mLogin.senha.getText().toString()) {
                resultado.setText("Usuario e senha corretos!");

            } else {
                resultado.setText("Usuario ou senha incorretos!");
            }
        }
    });
}


private static class login {
    EditText nome;
    EditText senha;
    Button button;
    EditText resultado;
}
}

Browser other questions tagged

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