google / gcm

Google Cloud Messaging - client libraries and sample implementations
https://developers.google.com/cloud-messaging
Apache License 2.0
824 stars 515 forks source link

NullPointerException for ErrorDialog Positive Action #209

Closed atakankaya closed 8 years ago

atakankaya commented 8 years ago

Hi,

On our launcher activity, we do the below check in order show a dialog stating that Google Play Services are not supported in case it is not available on the device.

private boolean checkPlayServices() {
    GoogleApiAvailability googleApi = GoogleApiAvailability.getInstance();

    int resultCode = googleApi.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (googleApi.isUserResolvableError(resultCode)) {
            googleApi.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
         } 

        return false;
    }
    return true;
}

On Genymotion emulators (and I guess on any rooted device where no Google Play Services is installed), we receive below crash upon clicking the OK button:

FATAL EXCEPTION: main
        Process: com.example.app, PID: 1703
        java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1505)
        at android.app.Activity.startActivityForResult(Activity.java:3917)
        at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
        at android.app.Activity.startActivityForResult(Activity.java:3877)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
        at com.google.android.gms.common.internal.zzi$1.zzasn(Unknown Source)
        at com.google.android.gms.common.internal.zzi.onClick(Unknown Source)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

And when I debug on android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871) I see that the argument intent is null. This issue started to happen when we switched from 7.3.0 to 9.2.0.

Is there any workaround for this?

prateekarora commented 8 years ago

Am seeing this too. Exact same NPE and stack trace because intent is null.

lucasnlm commented 8 years ago

A possible workaround is code below on the Activity you called googleApi.getErrorDialog.

@Override
public void startActivityForResult(Intent intent, int requestCode) {
    if (intent != null) {
        super.startActivityForResult(intent, requestCode);
    }
}

OR

@Override
public void startActivityForResult(Intent intent, int requestCode) {
    if (intent == null) {
        intent = new Intent();
    }

    super.startActivityForResult(intent, requestCode);
}

I prefer the second one.

ogeidix commented 8 years ago

Hi,

thanks for your report. I asked a colleague to investigate and I will report any update in the coming days.

atakankaya commented 8 years ago

I tested with the new release of GCM today and the problem still continues on 9.2.1

ogeidix commented 8 years ago

The bug has been identified. We will release a fix in a future release (unfortunately the release will happen in few months)

ogeidix commented 8 years ago

also please note that such error is generated when the device doesn't include Google Play Store.

The fix that will be released will prevent the NullPointerException exception, but the user will not be able to proceed anyway, since the lack of Google Play Store means that there is no way to install Google Play services.

fahimsakri-zz commented 8 years ago

Is this issue fixed with latest 9.4.0 release?

ogeidix commented 8 years ago

Yes! I don't see the release notes online yet, but 9.4.0 should include the fix.

dlukashev commented 8 years ago

Unfortunately it is still reproduced with 9.4.0. We had about 800 crashes yesterday. Probably because we swithed to firebase only.

compile 'com.google.firebase:firebase-ads:9.4.0' compile 'com.google.firebase:firebase-messaging:9.4.0' compile 'com.google.firebase:firebase-config:9.4.0'

Ali1067 commented 5 years ago

Hi, i'm unable to fix this issue as my playservices version is 16.0.0

ganta-music commented 2 years ago

It got fixed after adding this code in Manifest <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />