Google+ signout方法返回nullpointer异常

时间:2016-09-22 21:57:36

标签: java android nullpointerexception google-api google-login

我正在使用谷歌登录来登录用户。我在另一个活动中处理登录,并且我在另一个活动中有一个注销按钮。

    private void signOut() {
if(mGoogleApiClient.isConnected()){
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    loadloginActivity();
                }
            });

    }
    }

在上面的方法中,我正在退出用户并执行一个函数来引导用户进行登录活动。但是当我按下signout按钮时,我收到以下nullpointer错误。

    09-22 16:42:29.660 4459-4459/com.myapplication.tester E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.myapplication.tester, PID: 4459


java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.common.api.GoogleApiClient.isConnected()' on a null object reference
                                                                           at com.myapplication.tester.BaseActivity.signOut(BaseActivity.java:190)
                                                                           at com.myapplication.tester.BaseActivity.onOptionsItemSelected(BaseActivity.java:180)
                                                                           at android.app.Activity.onMenuItemSelected(Activity.java:2948)
                                                                           at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:406)
                                                                           at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
                                                                           at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
                                                                           at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
                                                                           at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
                                                                           at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203)
                                                                           at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:759)
                                                                           at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
                                                                           at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
                                                                           at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
                                                                           at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
                                                                           at android.widget.AdapterView.performItemClick(AdapterView.java:310)

如果有人可以帮我解决这个问题,我会很高兴。

编辑 - 我也试过没有检查GoogleApiClient连接,但我仍然得到nullpointer错误

public abstract class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private Toolbar mActionBarToolbar;
    private DrawerLayout mDrawerLayout;
    protected NavigationView mNavigationView;
    private ActionBarDrawerToggle mToggle;
    //Initialized mGoogleApiClient
    GoogleApiClient mGoogleApiClient;

    private GoogleApiClient client;

    protected boolean useToolbar() {
        return true;
    }

    protected boolean useDrawerToggle() {
        return true;
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        getActionBarToolbar();
        setupNavDrawer();
    }//end setContentView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }
              private void signOut() {   

            Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                    new ResultCallback<Status>() {
                        @Override
                        public void onResult(@NonNull Status status) {
                            loadloginActivity();
                        }
                    });


            }
        }

1 个答案:

答案 0 :(得分:0)

在您的应用程序类中声明mGoogleApiClient。如果您没有应用程序类,请将其创建为

public class MyApplication extends Application {
      public static GoogleApiClient mGoogleApiClient;
      @Override
      public void onCreate() {
           super.onCreate(); 
      }
}

并在Login Activity中实例化它。如

MyApplcation.mGoogleApiClient= new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API, Plus.PlusOptions.builder().build())
                .addScope(Plus.SCOPE_PLUS_LOGIN);

然后您可以使用相同的参考注销。 即,喜欢

 if (MyApplication.mGoogleApiClient.isConnected()) {
      //Your Logout Logic
       MyApplication.mGoogleApiClient.disconnect();
       MyApplication.mGoogleApiClient=null;
 }