13

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.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • Have you solved this? – Giru Bhai May 05 '17 at 13:36
  • Have you tryed to use `getApplicationContext()` method in stead of `App.getContext()`? Is this happening also if you use `.requestIdToken(getString(R.string.default_web_client_id))`? – Alex Mamo May 09 '17 at 17:25
  • @AlexMamo tried `getApplicationContext` with no luck – Giru Bhai May 10 '17 at 06:09
  • @GiruBhai, is you stack trace exactly the same? What version of `com.google.android.gms` do you compile against? – SergGr May 10 '17 at 06:44
  • @SergGr Thanks to reply, stacktrace is in http://stackoverflow.com/questions/43864774/googlesigninoptions-googleapiclient-bind-service-serviceconnection-crash and using latest `10.2.1` – Giru Bhai May 10 '17 at 07:00
  • Have you tryed to use `enableAutoManage` on `googleApiClient`? – Alex Mamo May 10 '17 at 07:10
  • @AlexMamo Yes, Please check my original question http://stackoverflow.com/questions/43864774/googlesigninoptions-googleapiclient-bind-service-serviceconnection-crash. – Giru Bhai May 10 '17 at 11:16
  • This could be another manifestation of the [bug](https://github.com/firebase/quickstart-android/issues/291), also reported [here](https://stackoverflow.com/questions/42362024/firebase-remote-config-nullpointerexception-on-map-keyset) – Alex Cohn Jan 14 '18 at 11:53

2 Answers2

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
pankaj yadav
  • 424
  • 4
  • 10
-1

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.

Ali Ahsan
  • 460
  • 3
  • 13