Null pointer exception in Android Google plus login
I'm a novice on Android. I integrated the Google plus login in my application. But it shows a null pointer exception in 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 for 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)
resolvent:
You are using the old Google sign method (before play services 8.3). If you build an application that uses the old version of Google to log in, you will build an application that tries to connect to Google apiclient. At this time, an account selector and / or a permission dialog box are displayed to the user, both of which will trigger the connection failure
You must handle these connection failures to log in. Once the Google API client connection is successful, the user is considered logged in and can be granted permission
Please refer to codelab by Google to handle such failures and use this method to implement login
Now the best choice is to try to use the new Google login with playback service 8.3. It has less friction and can automatically handle such connection failures for you
See new Google sign in
Please also check the official Android Developer blog for more information
Android Developers Blog- Identity #1
Android Developers Blog- Identity #2