我正在使用Google登錄來登錄用戶。我在另一個活動中處理登錄,並且在另一個活動中有一個登出按鈕。Google+登出方法返回空指針異常
private void signOut() {
if(mGoogleApiClient.isConnected()){
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
loadloginActivity();
}
});
}
}
在上面的方法中,我註銷用戶並執行一個函數來指導用戶登錄活動。但是當我按下登出按鈕時,我收到以下空指針錯誤。
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連接,但我仍然得到空指針錯誤
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();
}
});
}
}
你在哪裏初始化'mGoogleApiClient'? – Egor
在onCreate方法之前,我在類的開始處初始化了它。在代碼中添加了該代碼。謝謝。 – Aksh
這不是「初始化」。它只是聲明,默認值爲null。 – shmosel