退出Google帐户失败底部导航

问题描述

由于“退出”按钮位于片段上,因此我不能将“ this”用作“活动”,我可以用什么替换吗?

    GoogleSignInoptions gso = new GoogleSignInoptions.Builder(GoogleSignInoptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestemail()
        .build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

错误消息

error: no suitable method found for getClient(DashboardFragment,GoogleSignInoptions)
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this,gso);
                                                     ^
method GoogleSignIn.getClient(Context,GoogleSignInoptions) is not applicable
  (argument mismatch; DashboardFragment cannot be converted to Context)
method GoogleSignIn.getClient(Activity,GoogleSignInoptions) is not applicable
  (argument mismatch; DashboardFragment cannot be converted to Activity)

解决方法

GoogleSignIn.getClient(Context,GoogleSignInOptions)需要上下文而不是片段。

以这种方式使用它:

GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(getContext(),gso);

请注意,如果未附加该片段,则getContext()可能会返回null