gpt4 book ai didi

android - 如何在另一个 Fragment 中退出 Google 身份验证?

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:16 25 4
gpt4 key购买 nike

我正在使用 Google 身份验证登录。它将启动另一个 Java 的 Intent 。在此 fragment 中,我有一个注销按钮,但每次我想注销时,应用程序都会崩溃。你们能帮帮我吗? :)

我收到的错误信息是:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.google.android.gms.common.api.GoogleApiClient.getContext()' on a null object reference

代码:

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) {
// ...
}
});
}
}

最佳答案

需要初始化 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();
}

关于android - 如何在另一个 Fragment 中退出 Google 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44653287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com