【问题标题】:NullPointer Exception when logging out of Google Account退出 Google 帐户时出现 NullPointer 异常
【发布时间】:2015-01-09 02:26:46
【问题描述】:

已创建 Google 帐户登录/注销方法。主要思想是当用户点击

“使用 Google 登录”,它会将用户导航到用户个人资料页面,当用户决定注销时,将调用 Google 注销方法并将用户重定向回主页。但是,问题是当用户单击注销按钮时,出现以下错误,发生了什么?请帮忙

我附上了以下代码和错误日志

错误日志:

01-12 11:38:29.492: E/AndroidRuntime(20881): FATAL EXCEPTION: main
01-12 11:38:29.492: E/AndroidRuntime(20881): java.lang.IllegalStateException: GoogleApiClient must be connected.
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.google.android.gms.common.internal.n.a(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.google.android.gms.internal.no.a(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.google.android.gms.internal.no.clearDefaultAccount(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.dapoaugury.channelappdemo.ChannelAppMainActivity$DrawerItemClickListener.onItemClick(ChannelAppMainActivity.java:409)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AbsListView.performItemClick(AbsListView.java:1107)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2756)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.widget.AbsListView$1.run(AbsListView.java:3430)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.os.Handler.handleCallback(Handler.java:725)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.os.Looper.loop(Looper.java:137)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at java.lang.reflect.Method.invokeNative(Native Method)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at java.lang.reflect.Method.invoke(Method.java:511)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 11:38:29.492: E/AndroidRuntime(20881):    at dalvik.system.NativeStart.main(Native Method)

代码:

//注销:
案例2: //DAPO:DEV02-20141231:登录/注销选项的交替,当用户登录时登录更改为注销,反之亦然

            if (isLogin.equals("Login")){
                //If tab is login, user has not logged in, will navigate user to the login page and allow user to do a Google Login
                Intent intent = new Intent(getApplicationContext(),
                        ChannelAppLoginInfoMainActivity.class);
                startActivity(intent);
            }if (isLogin.equals("Logout")){
                //DAPO:DEV02:20150107:if tab is logout, will navigate user back to home page after user has logged out of Google account.

                Toast.makeText(getApplicationContext(), "Logging out of ChannelApp!", Toast.LENGTH_LONG).show();

                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();

                Intent intent= new Intent(getApplicationContext(),
                        ChannelAppMainActivity.class);
                startActivity(intent);
            }
            break;

编辑代码:

//DAPO:DEV02-20150108: Declare Google variable:Google+client
private GoogleApiClient mGoogleApiClient;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//DAPO:DEV02-20150107: Initialize GoogleApiClient variable
    mGoogleApiClient= new GoogleApiClient.Builder(this).addApi(Plus.API).
            addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
//DAPO:DEV02-20150110: Invoking of GoogleApiClient and connecting GoogleApiClient
@Override
protected void onStart() {
    super.onStart();
    // Connect To Drive and Google+
    mGoogleApiClient.connect();
  }
@Override
protected void onStop(){
    super.onStop();
    // Disconnect from Drive and Google+
    mGoogleApiClient.disconnect();
}

protected void onConnected(Bundle ConnectionHint){
    //All Clients are connected
    Intent intent = new Intent(getApplicationContext(),
            ChannelAppAbstractGetNameTask.class);
    startActivity(intent);
}
//DAPO:DEV02-20150110: End of Edited Version of Invoking of GoogleApiClient and connecting GoogleApiClient

【问题讨论】:

  • ChannelAppMainActivity.java 中的第 377 行是哪一个?
  • @Rohit5k2 是这一行“mPlusClient.clearDefaultAccount();”
  • 哦...mPlusClient 为空。请发布ChannelAppMainActivity活动的代码
  • @Rohit5k2,ChannelAppMainActivity活动的代码是什么?上面的代码是ChannelAppMainActivity的一部分。我已经初始化了 mPLusCLient
  • 如果不查看代码,我将无法告诉您为什么它为空,以及为什么我需要该活动的完整代码。你加上客户端对象以某种方式变为空。

标签: android google-api google-plus google-api-client google-signin


【解决方案1】:
else{ 
            mPlusClient.clearDefaultAccount();
            mPlusClient.disconnect();
            mPlusClient.connect();
            Intent intent= new Intent(getApplicationContext(),
                    ChannelAppMainActivity.class); 
            startActivity(intent);
        }   

改一下

else{ 
            mPlusClient.clearDefaultAccount();
            mPlusClient.disconnect();
            mPlusClient.connect();
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        } 

【讨论】:

  • 你如何解释这段代码?当你没有说明意图路径时,意图是什么?
  • 您正在重新创建您的活动,以便您在注销后重新创建并使用登录按钮返回相同的活动
  • 尝试您的解决方案后仍然出现空指针错误
  • 不启动活动检查不知道启动活动是否退出
  • 你将如何初始化 mPLusClient 变量??显然,这是不正确的
【解决方案2】:

设法排查并发现了一直困扰项目的问题:

初始问题:

  1. GoogleApiClient 不断提示连接
  2. 活动不断将用户导航到没有用户 Google 凭据的空用户个人资料页面

解决方案:

  1. 为 GoogleApiClient 声明参数(代码如下)
  2. 从注销方法中删除了Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);,因为用户首次访问应用时没有默认帐户。

更新代码:

mGoogleApiClient= new GoogleApiClient.Builder(this).addApi(Plus.API).
            addScope(Plus.SCOPE_PLUS_LOGIN).build();

//DAPO:DEV02-20150110: Invoking of GoogleApiClient and connecting GoogleApiClient

@Override
protected void onStart() {
    super.onStart();
    // Connect To Drive and Google+
    mGoogleApiClient.connect();
  }
@Override
protected void onStop(){
    super.onStop();
    // Disconnect from Drive and Google+
    if(mGoogleApiClient.isConnected()){
    mGoogleApiClient.disconnect();
    }
}

protected void onConnected(Bundle ConnectionHint){
    //All Clients are connected
    mSignInClicked=false;
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}

/* A helper method to resolve the current ConnectionResult error. */
private void resolveSignInError() {
  if (mConnectionResult.hasResolution()) {
    try {
      mIntentInProgress = true;
      startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
          RC_SIGN_IN, null, 0, 0, 0);
    } catch (SendIntentException e) {
      // The intent was canceled before it was sent.  Return to the default
      // state and attempt to connect to get an updated ConnectionResult.
      mIntentInProgress = false;
      mGoogleApiClient.connect();
    }
  }
}
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();
        }
      }
    }
//DAPO:DEV02-20150110: End of Edited Version of Invoking of GoogleApiClient and connecting GoogleApiClient

//Logout:   
        case 2:
            //DAPO:DEV02-20141231: alternation of login/logout options, login to change to logout when user is login and vice versa

            if (isLogin.equals("Login")){
                //If tab is login, user has not logged in, will navigate user to the login page and allow user to do a Google Login
                Intent intent = new Intent(getApplicationContext(),
                        ChannelAppLoginInfoMainActivity.class);
                startActivity(intent);
            }if (isLogin.equals("Logout")){
                //DAPO:DEV02:20150107:if tab is logout, will navigate user back to home page after user has logged out of Google account.

                Toast.makeText(getApplicationContext(), "Logging out of ChannelApp!", Toast.LENGTH_LONG).show();

                //To disconnect user from GoogleApiClient server, remove all Google login credentials.
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();

                //Return user to Main HomePage
                startActivity(new Intent(getApplicationContext(), ChannelAppMainActivity.class));
                // make sure the user can not access the page after he/she is logged out
                // clear the activity stack
                finish();
            }
            break;
            //DAPO:DEV02-20141231: End of Edited Version to implement GoogleApiClient logout method and implementation for login/logout method.

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多