Error in android.widget.Button.setOnClickListener, "Attempt to invoke virtual method"

1

Why do I have the following error:

  

Attempt to invoke virtual method ... on a null object reference

the signIn.java is

    public class SignIn extends AppCompatActivity {

    EditText edtPhone,edtPassword;
    Button btnSignIn;

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

        edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
        edtPhone = (MaterialEditText)findViewById(R.id.edtPhone);
        btnSignIn = (Button)findViewById(R.id.btnSignIn);

        //init firebase

        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference table_user = database.getReference("Usuario");

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final ProgressDialog mDialog = new ProgressDialog(SignIn.this);
                mDialog.setMessage("Por favor espera...");
                mDialog.show();


                table_user.addValueEventListener(new ValueEventListener() {





                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        //comprobar si el usuario no existe en la base de datos
                        if (dataSnapshot.child(edtPhone.getText().toString()).exists()) {

                            //Informacion usuario

                            mDialog.dismiss();
                            Usuario usuario = dataSnapshot.child(edtPhone.getText().toString()).getValue(Usuario.class);
                            if (usuario.getContraseña().equals(edtPassword.getText().toString())) {
                                Toast.makeText(SignIn.this, "Inicio de Sesión Exitoso !", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(SignIn.this, "Inicio de Sesión Fallido !!", Toast.LENGTH_SHORT).show();
                            }
                        }
                        else
                        {
                            Toast.makeText(SignIn.this, "Usuario no registrado en base de dato", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
            }
        });

    }
}

The xml Code button

<Button
    android:id="@+id/btnSignIn1"
    android:text="@string/SignIn"
    android:textColor="@android:color/white"
    android:background="@drawable/buttonstyle1"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="460dp"

    />

Thanks !!.

    
asked by Neil 12.11.2017 в 15:14
source

1 answer

1

When trying to obtain the reference of the button, it does not exist, therefore btnSignIn has value null

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

If the button has a null value, you can not call a method, for this reason you get the error.

  

Attempt to invoke virtual method ... on a null object reference

You should check that the button with id btnSignIn is actually in the layout that loads your activity which is: activity_sign_in.xml , if the button is in the layout, it ensures that its id is correct.

    
answered by 12.11.2017 / 15:25
source