集成 g+ Signin 时出现 NULL 指针异常

NULL Pointer Exception when integrating g+ Signin

这是我的代码

public class gsign extends Activity implements ConnectionCallbacks,OnConnectionFailedListener {
private static final int RC_SIGN_IN = 0;
private static final String TAG = "MainActivity";
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
            .addConnectionCallbacks(this).addOnConnectionFailedListener(this)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

   }

protected void onStart() {
    super.onStart();
    signInWithGplus();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
public void onConnectionFailed(ConnectionResult result) {
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                0).show();
        return;
    }
    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;
        if (mSignInClicked) {
            resolveSignInError();
        }
    }
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
                                Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        if (responseCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

@Override
public void onConnected(Bundle arg0) {
    mSignInClicked = false;
    // Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
    getProfileInformation();
}

@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();

}
private void signInWithGplus() {
    if (!mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        resolveSignInError();
    }
    else
        getProfileInformation();
}

/**
 * Method to resolve any signin errors
 * */
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        } catch (IntentSender.SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
  }
}

我也有代码在我的云后端注册用户并获取他们的信息,但我收到以下错误

Caused by: java.lang.NullPointerException
        at    com.example.nirmal.loginpage.gsign.resolveSignInError(gsign.java:127)
        at com.example.nirmal.loginpage.gsign.signInWithGplus(gsign.java:117)
        at com.example.nirmal.loginpage.gsign.onCreate(gsign.java:56)

下面的错误指向if (mConnectionResult.hasResolution())

通常这个错误可能是在从 onCreate() 方法调用 signInWithGPlus() 时引起的,但即使在从 onStart() 调用它之后我也遇到了同样的错误。 问题是我在另一个应用程序中使用了相同的代码(实际上我是从那个应用程序复制的),其中 运行 完美所以我不知道是什么导致了这个错误。

我还为此应用创建了单独的 OAuth 客户端密钥。那么谁能指出我哪里出错了????

堆栈跟踪说明了一切...

您在 onStart() 之前不会打电话给 mGoogleApiClient.connect()。同时返回 onCreate(),在 onStart() 之前调用,您调用 signInWithGplus(); 方法。

该方法中的

!mGoogleApiClient.isConnecting() 为真,导致 resolveSignInError() 被调用。

您尚未为 mConnectionResult 赋值,因此在 resolveSignInError() 中调用 mConnectionResult.hasResolution() 将抛出 NullPointerException.