Rare NullPointerException in GoogleApiClient using

2020-05-26 10:36发布

问题:

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.

回答1:

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


回答2:

While checking for Google Play services would be good, I think this may be due to a device having an old version of Google Play services.but that's not a great error regardless.