GameHelper crashes on onConnectionFailed()

2020-03-13 08:45发布

问题:

I've got the following crash in GameHelper.java:

[main] java.lang.NullPointerException at com.google.android.gms.common.ConnectionResult.startResolutionForResult(Unknown Source) at com.google.example.games.basegameutils.GameHelper.resolveConnectionResult(GameHelper.java:752) at com.google.example.games.basegameutils.GameHelper.onConnectionFailed(GameHelper.java:729)

The only reason I think that could happen is if mActivity == null at GameHelper.java:752:

mConnectionResult.startResolutionForResult(mActivity, RC_RESOLVE);

mActivity gets null on onStop() Is it possible that GameHelper.java has bug and can crash if onConnectionFailed() happens after onStop() is called? Thanks.

EDITED: It happened after update to the latest Play API (rev 15) together with the updated GameHelper.java.

回答1:

EDIT:

This now has been fixed in latest GameHelper version: https://github.com/playgameservices/android-samples/commit/e7b3758c136b5b434f1dfd9ca8c03f75aad70f09

OLD ANSWER:

For me it happens on the start of the app, when Google Play Services asks me to sign in and if I click cancel, this same error happens.

So when leaving from your own activity to sign in activity, it dispatches onStop event, and fails to connect because of the user initiated process, which is resolvable, thus the error happens.

So my quick hack was changing:

catch (SendIntentException e)

to simply

catch (Exception e)

So it would also catch Null pointer exception Of course in this case the signup process might not proceed, so I initate relogin on another action and it seems to work for now.

More thorough hack could be trying to resolve the result on activity start, for that we define pending resolution variable:

// Are we expecting the result of a resolution flow?
boolean mExpectingResolution = false;
boolean mPendingResolution = false;

Then on the error line we check if activity is not null

if(mActivity != null)
    mConnectionResult.startResolutionForResult(mActivity, RC_RESOLVE);
else
    mPendingResolution = true;

And on start we check and try to resolve it:

if(mPendingResolution && mConnectionResult != null)
try {
    mConnectionResult.startResolutionForResult(mActivity, RC_RESOLVE);
} catch (SendIntentException e) {
    e.printStackTrace();
}

This should help until the official resolution from lib supporters :)



回答2:

Today is 16th september 2014 and I still facing this problem.

I don't know why anyone else did not answer about to comment GameHelper line. In onStop method there is a line to set mActivity variable as null.

I commented this line (like below) and my app is working properly.

  /** Call this method from your Activity's onStop(). */
public void onStop() {
    debugLog("onStop");
    assertConfigured("onStop");
    if (mGoogleApiClient.isConnected()) {
        debugLog("Disconnecting client due to onStop");
        mGoogleApiClient.disconnect();
    } else {
        debugLog("Client already disconnected when we got onStop.");
    }
    mConnecting = false;
    mExpectingResolution = false;



    // let go of the Activity reference
    //mActivity = null;  //COMMENT THIS LINE!!!!
    //COMMENT ABOVE LINE
}

Is there any problem doing that:?