I'm running my app on Genymotion, developing on Android Studio.
Every time I run my application I click on the button to register, but when I click, I find the following message "Unfortunately MyApp has stopped", where after the application is finished.
Note: I am using Firebase to register data
Code of my Activity
package com.example.matheus.MyApp;
import android.content.Intent;
import android.support.annotation.NonNull;
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 com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class CadastroActivity extends AppCompatActivity {
private Button mCadastro;
private EditText mEmail, mSenha;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadastro);
mAuth = FirebaseAuth.getInstance();
firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user !=null){
Intent intent = new Intent(CadastroActivity.this, MainActivity.class);
startActivity(intent);
finish();
return;
}
}
};
mCadastro = (Button) findViewById(R.id.cadastrar);
mEmail = (EditText) findViewById(R.id.email);
mSenha = (EditText) findViewById(R.id.senha);
mCadastro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = mEmail.getText().toString();
final String senha = mSenha.getText().toString();
//linha 56 AQUI mAuth.createUserWithEmailAndPassword(email, senha).addOnCompleteListener(CadastroActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()){
Toast.makeText(CadastroActivity.this, "Cadastrado com sucesso! Efetue o login", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(firebaseAuthStateListener);
}
@Override
protected void onStop() {
super.onStop();
mAuth.removeAuthStateListener(firebaseAuthStateListener);
}
}
Code where you detail where the error is:
02-17 13: 27: 45,439 1961-1961 / com.example.matheus.MyApp E / AndroidRuntime: FATAL EXCEPTION: main Process: com.example.matheus.MyApp, PID: 1961 java.lang.NullPointerException at com.google.android.gms.internal.zzdtp.zzb (Unknown Source) at com.google.android.gms.internal.zzdtw.zza (Unknown Source) at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword (Unknown Source) at com.example.matheus.MyApp.CadastroActivity $ 2.onClick (CadastroActivity.java:56) at android.view.View.performClick (View.java:4438) at android.view.View $ PerformClick.run (View.java:18422) at android.os.Handler.handleCallback (Handler.java:733) at android.os.Handler.dispatchMessage (Handler.java:95) at android.os.Looper.loop (Looper.java:136) at android.app.ActivityThread.main (ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:601) at dalvik.system.NativeStart.main (Native Method)