Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rare NullPointerException in GoogleApiClient using Firebase

I am using GoogleApiClient to login users using their Google account. Basically, I am using Firebase Auth with Google sign in.

But I am getting this crash on some devices every single day. When I test on some of my own devices (OnePlus 3, Nexus 5X, Moto G, etc) I never see this crash.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase(java.util.Locale)' on a null object reference
       at com.google.android.gms.internal.zzamy.zzd(Unknown Source)
       at com.google.android.gms.internal.zzamv.n(Unknown Source)
       at com.google.android.gms.internal.zzamv.zza(Unknown Source)
       at com.google.android.gms.internal.zzamv$2.run(Unknown Source)
       at java.lang.Thread.run(Thread.java:818)

This is what I am doing to instantiate the GoogleApiClient. I don't see the crash in normal situations but in some devices I am seeing this. Here is what I am doing in code,

String mClientId = parcel.getProviderExtra().getString(CLIENT_ID_KEY);
GoogleSignInOptions googleSignInOptions;

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(mClientId)
                .requestEmail()
                .build();

mGoogleApiClient = new GoogleApiClient.Builder(App.getContext())
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                .build();

mGoogleApiClient.connect();

What am I doing wrong here? My best bet is, App.getContext() which gives the application context to instantiate the client. Can this be the problem?

I can use the activity context, but using that leads to a memory leak. What is the problem here and how can it be solved?

This is creating a very poor experience to some users who get a crash just after opening the app and trying to sing in.

like image 835
Aritra Roy Avatar asked Nov 28 '16 14:11

Aritra Roy


1 Answers

Hello You can try this code this is a working code in my project first initialize google api client in on create

public void initGPlus() {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}

and on the button click call another function

 Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);

In On Activity result just got your response and handle that result in a function

if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }

implement this interface in your activity

 implements GoogleApiClient.OnConnectionFailedListener
like image 131
pankaj yadav Avatar answered Sep 30 '22 18:09

pankaj yadav