Error "Unable to instantiate activity" when executing Activity

1

My application has a home screen, where the user can select the registration screen. However, it is only me because of the gets that it stops working with this error below, it does not even show the registration activity.

The error is as follows:

  

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo {com.example.romulo.fateclins / com.example.romulo.fateclins.CadastroActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view. Window $ Callback android.view.Window.getCallback () 'on a null object reference

It only appears when I put the gets, of the fields in the class of this activity:

package com.example.romulo.fateclins;

import android.content.Context;
import android.content.Intent;
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;

import static android.R.attr.button;
import static android.R.attr.duration;
import static android.R.attr.publicKey;
import static com.example.romulo.fateclins.R.layout.activity_cadastro;
import static com.example.romulo.fateclins.R.layout.activity_login;

public class CadastroActivity extends AppCompatActivity {
private Usuarios usuarios;
    private EditText editTextNome = (EditText) findViewById(R.id.editTextNome);
    private EditText editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    private EditText editTextCurso = (EditText) findViewById(R.id.editTextCurso);
    private EditText editTextSemestre = (EditText) findViewById(R.id.editTextSemestre);
    private EditText editTextRA = (EditText) findViewById(R.id.editTextRA);
    private EditText editTextUsername = (EditText) findViewById(R.id.editTextUsername);
    private EditText editTextSenha = (EditText) findViewById(R.id.editTextSenha);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cadastro);
        this.usuarios = new Usuarios();
        Button botaocadastrar = (Button) findViewById(R.id.buttonCadastrar);
        botaocadastrar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                cadastrar();

            }
        });
    }



    public void cadastrar (){


        this.usuarios.setNOME(this.editTextNome.getText().toString());
        this.usuarios.setEMAIL(this.editTextEmail.getText().toString());
        this.usuarios.setCURSO(this.editTextCurso.getText().toString());
        this.usuarios.setSEMESTRE(this.editTextSemestre.getText().toString());
        this.usuarios.setRA(this.editTextRA.getText().toString());
        this.usuarios.setUSERNARME(this.editTextUsername.getText().toString());
        this.usuarios.setPASSWORD_2(this.editTextSenha.getText().toString());
        this.usuarios.cadastrar();

        Toast.makeText(this,this.usuarios.get_mensagem(),Toast.LENGTH_LONG).show();
        if (usuarios.is_status()){
            finish();
        } else{
            finish();
        }
    }
}
    
asked by anonymous 21.11.2016 / 00:56

1 answer

1

Initialize the EditText within the OnCreate method:

private Usuarios usuarios;
private EditText editTextNome;
private EditText editTextEmail;
private EditText editTextCurso;
private EditText editTextSemestre;
private EditText editTextRA;
private EditText editTextUsername;
private EditText editTextSenha;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro);


    editTextNome = (EditText) findViewById(R.id.editTextNome);
    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    editTextCurso = (EditText) findViewById(R.id.editTextCurso);
    editTextSemestre = (EditText) findViewById(R.id.editTextSemestre);
    editTextRA = (EditText) findViewById(R.id.editTextRA);
    editTextUsername = (EditText) findViewById(R.id.editTextUsername);
    editTextSenha = (EditText) findViewById(R.id.editTextSenha);

    this.usuarios = new Usuarios();
    Button botaocadastrar = (Button) findViewById(R.id.buttonCadastrar);
    botaocadastrar.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            cadastrar();

        }
    });
}
    
21.11.2016 / 09:27