3

I am new to android. I am integrating google plus login in my application. But it shows null pointer exception at mConnectionResult.hasResolution This is my code

 btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
    btnSignIn.setOnClickListener(this);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        if (resultCode != RESULT_OK) {
            mSignInClicked = false;
        }

        mIntentInProgress = false;

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

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

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

/**
 * 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();
        }
    }
}

@Override
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) {
            // The user has already clicked 'sign-in' so we attempt to
            // resolve all
            // errors until the user is signed in, or they cancel.
            resolveSignInError();
        }
    }

}

@Override
public void onConnected(Bundle arg0) {
    mSignInClicked = false;
    if (temp == true) {
        type = "2";
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

        Intent i = new Intent(RegisterSkipActivity.this, MainActivity.class);
        i.putExtra("Login", type);
        startActivity(i);
        temp = false;
    }

}


@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();
    //updateUI(false);
}

@Override
public void onClick(View v) {

    // Signin button clicked
    signInWithGplus();
    temp = true;

}

/**
 * Sign-in into google
 */
private void signInWithGplus() {
    if (!mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        resolveSignInError();
    }
}

Nullpointer exception at this line in resolveSignInError()

  if (mConnectionResult.hasResolution()) 

Please help me. This is Logcat

  java.lang.NullPointerException
        at com.techieweb.solutions.pickeronline.RegisterSkipActivity.resolveSignInError(RegisterSkipActivity.java:214)
        at com.techieweb.solutions.pickeronline.RegisterSkipActivity.signInWithGplus(RegisterSkipActivity.java:284)
        at com.techieweb.solutions.pickeronline.RegisterSkipActivity.onClick(RegisterSkipActivity.java:273)
        at com.google.android.gms.common.SignInButton.onClick(Unknown Source)
        at android.view.View.performClick(View.java:4439)
        at android.widget.Button.performClick(Button.java:139)
        at android.view.View$PerformClick.run(View.java:18395)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
USER9561
  • 1,084
  • 3
  • 15
  • 41

2 Answers2

3

You are using the old Google-Sign Method(before play services 8.3). If you built an app that used the old Sign-In with Google, you would build one that attempted to connect to a GoogleApiClient. At this point the user was presented with an account picker and/or a permissions dialog, both of which would trigger a connection failure.

You would have to handle these connection failures in order to sign in. Once the GoogleApiClient connected, then the user was considered to be signed in and the permissions could be granted.

Refer this codelab by google to handle that kind of failures and implement the sign in using that method.

The best option now is to try out the new Google-Sign In with play services 8.3. It's more friction-less and automatically handles these kind of connection failures for you.

Refer new Google-Sign In.

Also check out official android developers blog to get more info.

Android Developers Blog- Identity #1

Android Developers Blog- Identity #2

vishnu narayanan
  • 3,813
  • 2
  • 24
  • 28
0

You are initializing mConnectionResult in OnConnectionFailed callback only. resolveSignInError() method is getting called even before this callback, hence the mConnectionResult is null.

You can either do a null check, or make sure your callback method is fired prior. To confirm just add logs in onConnectionFailed() and in resolveSignInError(). You will notice which is getting called first.

Henry
  • 17,490
  • 7
  • 63
  • 98