0

我使用Google身份驗證登錄。它將啓動另一個Java的意圖。在這個片段中,我有一個Sign out按鈕,但每次我想退出時,該應用程序都會崩潰。 你們能幫我嗎? :)如何在另一個片段中通過Google身份驗證註銷?

我越來越

的錯誤信息是:

顯示java.lang.NullPointerException:試圖調用虛擬方法android.content.Context com.google.android.gms.common.api.GoogleApiClient .getContext()」上的空對象引用

代碼:

public class Setttings extends Activity { 

private Button msigno; 
private FirebaseAuth mAuth; 

private GoogleApiClient mGoogleApiClient; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.settings); 


    DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 

    int width = dm.widthPixels; 
    int height = dm.widthPixels; 

    getWindow().setLayout((int)(width*0.95),(int) (height*0.93)); 

    msigno = (Button) findViewById(R.id.signout); 

    msigno.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mAuth = FirebaseAuth.getInstance(); 

      switch (v.getId()) { 
       // ... 
       case R.id.signout: 
        signOut(); 
        break; 
       // ... 

      } 
      mAuth.signOut(); 

      // Google sign out 
      Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
        new ResultCallback<Status>() { 
         @Override 
         public void onResult(@NonNull Status status) { 
         } 
        }); 
      Intent intent2 = new Intent(Setttings.this, MainActivity.class); 
      startActivity(intent2); 

     } 
    }); 



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

回答

1

需要初始化API客戶端退出。

@Override 
protected void onStart() { 
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail() 
      .build(); 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 
相關問題