دنبال کننده ها

۱۳۹۶ اسفند ۱۱, جمعه

Android - NullPointerException Attempt to invoke virtual method 'com.google.android.gms.tasks.Task

[ad_1]



Trying to set up a simple app that uses Firebase Auth to handle Registration/Login of a user. This issue is quite confusing as the outcome is different on my physical testing device (HTC M8 7.1.1) and Emulator (5P 7.1.1).



On the emulator, I get the following error when I click my Login or Register Buttons.



 --------- beginning of crash
03-02 03:24:20.625 3002-3002/com.mad.losesano E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mad.losesano, PID: 3002
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdd)' on a null object reference
at com.google.android.gms.internal.zzdvv.zzb(Unknown Source)
at com.google.android.gms.internal.zzdwc.zzb(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source)
at com.mad.losesano.LoginActivity.userLogin(LoginActivity.java:69)
at com.mad.losesano.LoginActivity.onClick(LoginActivity.java:105)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)


On my physical device, when I register a user, it works as expected. The user account can be seen in the firebase console. When I click 'login' on the this device, nothing happens. It seems that it doesn't even enter the OnClick method (tested by running a Log.d message in the OnClick method which doesn't show up when I click the button).



Here is the LoginActivity Code



package com.mad.losesano;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
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;

public class LoginActivity extends Activity implements View.OnClickListener

FirebaseAuth mAuth;
EditText et_email, et_password;
Button login_button;

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

mAuth = FirebaseAuth.getInstance();

et_email = (EditText) findViewById(R.id.email_et_login);
et_password = (EditText) findViewById(R.id.password_et_login);

findViewById(R.id.register_tv).setOnClickListener(this);
login_button = (Button) findViewById(R.id.login_button);
login_button.setOnClickListener(this);



private void userLogin()
String email = et_email.getText().toString().trim();
String password = et_password.getText().toString().trim();

if (email.isEmpty())
et_email.setError("Email is required");
et_email.requestFocus();
return;


if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
et_email.setError("Please enter a valid email");
et_email.requestFocus();
return;


if (password.isEmpty())
et_password.setError("Password is required");
et_password.requestFocus();
return;


if (password.length() < 6)
et_password.setError("Minimum lenght of password should be 6");
et_password.requestFocus();
return;


mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if (task.isSuccessful())
finish();
Intent intent = new Intent(LoginActivity.this, LoggedInActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
else
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();


);


@Override
protected void onStart()
super.onStart();

if (mAuth.getCurrentUser() != null)
finish();
startActivity(new Intent(this, LoggedInActivity.class));



@Override
public void onClick(View view)
switch (view.getId())
case R.id.register_tv:
finish();
Log.d("LoSeSANO", "Register Button Clicked");
startActivity(new Intent(LoginActivity.this, SignupActivity.class));
break;

case R.id.login_button:
Log.d("LoSeSANO", "Login Button Clicked");
userLogin();
break;





Here is the SignupActivity code



package com.mad.losesano;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.util.Patterns;
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.FirebaseAuthUserCollisionException;

/**
* Created by oshau on 01/03/2018.
*/

public class SignupActivity extends Activity implements View.OnClickListener

EditText et_email, et_password;
Button register_button;
private FirebaseAuth mAuth;

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

et_email = (EditText) findViewById(R.id.email_et_login);
et_password = (EditText) findViewById(R.id.password_et_login);

mAuth = FirebaseAuth.getInstance();

register_button = (Button) findViewById(R.id.register_button);
register_button.setOnClickListener(this);
findViewById(R.id.login_tv).setOnClickListener(this);



private void registerUser()
Log.d("LoSeSANO", "Email Address: " + et_email.getText().toString());
String email = et_email.getText().toString().trim();
String password = et_password.getText().toString().trim();

if (email.isEmpty())
et_email.setError("Email is required");
et_email.requestFocus();
return;


if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
et_email.setError("Please enter a valid email");
et_email.requestFocus();
return;


if (password.isEmpty())
et_password.setError("Password is required");
et_password.requestFocus();
return;


if (password.length() < 6)
et_password.setError("Minimum lenght of password should be 6");
et_password.requestFocus();
return;


mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if (task.isSuccessful())
finish();
startActivity(new Intent(SignupActivity.this, LoginActivity.class));
else

if (task.getException() instanceof FirebaseAuthUserCollisionException)
Toast.makeText(getApplicationContext(), "You are already registered", Toast.LENGTH_SHORT).show();

else
Toast.makeText(getApplicationContext(), task.getException().getMessage(), Toast.LENGTH_SHORT).show();




);



@Override
public void onClick(View view)
switch (view.getId())
case R.id.register_button:
registerUser();
break;

case R.id.login_tv:
finish();
startActivity(new Intent(SignupActivity.this, LoginActivity.class));
break;





Here is my login_activity.xml and signup_activity.xml if needed



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/welcome_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/welcome_string"
android:textColor="#f0eeec"
android:textSize="40dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="80dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="202dp"
android:layout_height="62dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:text="@string/sub_welcome_string"
android:textColor="#f0eeec"
android:textSize="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/welcome_tv" />

<EditText
android:id="@+id/email_et_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/holo_green_light"
android:ems="10"
android:fontFamily="sans-serif-light"
android:inputType="textPersonName"
android:text="@string/register_et_string"
android:textColor="#f0eeec"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<EditText
android:id="@+id/password_et_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/holo_green_light"
android:ems="10"
android:fontFamily="sans-serif-light"
android:hint="Password"
android:inputType="textPassword"
android:text="Password"
android:textColor="#f0eeec"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email_et_login" />

<Button
android:id="@+id/login_button"
android:layout_width="151dp"
android:layout_height="55dp"
android:layout_marginTop="16dp"
android:backgroundTint="#6c8c0e"
android:fontFamily="sans-serif-light"
android:text="@string/login_register_button_string"
android:textColor="#f0eeec"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password_et_login" />

<TextView
android:id="@+id/register_tv"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_marginTop="8dp"
android:backgroundTint="#6c8c0e"
android:fontFamily="sans-serif-light"
android:text="@string/register_tv_string"
android:textColor="#f0eeec"
android:textSize="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/login_button" />

<TextView
android:id="@+id/password_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:backgroundTint="#6c8c0e"
android:fontFamily="sans-serif-light"
android:text="@string/forgot_password_tv_string"
android:textColor="#f0eeec"
android:textSize="16dp"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/register_tv" />

</android.support.constraint.ConstraintLayout>


signup_activity



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/welcome_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:text="@string/welcome_string"
android:textColor="#f0eeec"
android:textSize="40dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="80dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="202dp"
android:layout_height="62dp"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:text="@string/sub_welcome_string"
android:textColor="#f0eeec"
android:textSize="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/welcome_tv" />

<EditText
android:id="@+id/email_et_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/holo_green_light"
android:ems="10"
android:fontFamily="sans-serif-light"
android:inputType="textPersonName"
android:text="@string/register_et_string"
android:textColor="#f0eeec"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<EditText
android:id="@+id/password_et_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/holo_green_light"
android:ems="10"
android:fontFamily="sans-serif-light"
android:hint="Password"
android:inputType="textPassword"
android:text="Password"
android:textColor="#f0eeec"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email_et_login" />

<Button
android:id="@+id/register_button"
android:layout_width="151dp"
android:layout_height="55dp"
android:layout_marginTop="16dp"
android:backgroundTint="#6c8c0e"
android:fontFamily="sans-serif-light"
android:text="@string/register_tv_string"
android:textColor="#f0eeec"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password_et_login" />

<TextView
android:id="@+id/login_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:backgroundTint="#6c8c0e"
android:fontFamily="sans-serif-light"
android:text="@string/login_register_button_string"
android:textColor="#f0eeec"
android:textSize="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/register_button" />

</android.support.constraint.ConstraintLayout>


Any help would be greatly appreciated!




[ad_2]

لینک منبع