0

I am using the google login to login the user. I handle login in another activity and I have a signout button in another activity.

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

    }
    }

In the method above, I am signing out the user and executing a function to direct the user to the login activity. But when I press the signout button, I am getting the following nullpointer error.

    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)

I would be glad if someone can help me fix this issue.

Edit - I also tried without checking the GoogleApiClient connection but I am still getting nullpointer error

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


            }
        }
Aksh
  • 23
  • 2
  • 5
  • Where do you initialize `mGoogleApiClient`? – Egor Sep 22 '16 at 22:40
  • I initialized at the beginning of the class before the onCreate method. Added that on the code. Thanks. – Aksh Sep 23 '16 at 02:28
  • It's not "initialized". It's just declared, with the default value of null. – shmosel Sep 23 '16 at 05:12
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – shmosel Sep 23 '16 at 05:12

1 Answers1

0

Declare the mGoogleApiClient in your application class. If you don't have an application class, create it as

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

And instantiate it in the Login Activity. as

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

you can then use the same reference to logout. i.e , Like

 if (MyApplication.mGoogleApiClient.isConnected()) {
      //Your Logout Logic
       MyApplication.mGoogleApiClient.disconnect();
       MyApplication.mGoogleApiClient=null;
 }
Tchinmai
  • 620
  • 1
  • 6
  • 18