"Unable to instantiate Activity" error while running Activity

Asked

Viewed 298 times

1

My application has a home screen, where the user can select the registration screen. However, it is only me to put the gets that it stops working with this error below, does not 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 appears only when I put the gets, of the fields in the registration 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();
        }
    }
}
  • Although you are starting the components, probably the text of some of them is null. There, as you call the direct toString without verifying the nullity can cause the NPE. I suggest you put in sets something like: (component.gettext() != null ? component.gettext().toString) "". I didn’t put it out as an answer because I’m guessing it’s based on the code snippet you put in.

  • Hello, Giuliana, thank you so much for your help!

1 answer

1


Initialize 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();

        }
    });
}
  • Hi Kleiser, thank you so much man! It worked!

Browser other questions tagged

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