其他分享
首页 > 其他分享> > android-谷歌加登录中的空指针异常

android-谷歌加登录中的空指针异常

作者:互联网

我是android新手.我在我的应用程序中集成了google plus登录名.但是它在mConnectionResult.hasResolution中显示了空指针异常
这是我的代码

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

resolveSignInError()中此行的Nullpointer异常

  if (mConnectionResult.hasResolution()) 

请帮我.这是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)

解决方法:

您正在使用旧的Google Sign方法(在play services 8.3之前).如果您构建了一个使用旧版Google登录的应用,那么您将构建一个尝试连接到GoogleApiClient的应用.此时,向用户显示了一个帐户选择器和/或一个权限对话框,这两者都会触发连接失败.

您必须处理这些连接失败才能登录.一旦GoogleApiClient连接成功,便认为该用户已登录,并且可以授予权限.

请参考codelab by google 以处理此类故障并使用该方法实施登录.

现在最好的选择是尝试使用带播放服务8.3的新Google登录.它的摩擦更少,并且可以自动为您处理此类连接故障.

请参阅new Google-Sign In.

另请查看官方android开发人员博客以获取更多信息.

Android Developers Blog- Identity #1

Android Developers Blog- Identity #2

标签:google-plus-signin,nullpointerexception,android
来源: https://codeday.me/bug/20191119/2035359.html