已创建 Google 帐户登录/注销方法。主要思想是当用户点击
“使用 Google 登录”,它将用户导航到用户配置文件页面,当用户决定注销时,将调用 Google 注销方法并将用户重定向回主页。但是,问题是当用户点击注销按钮时,会出现以下错误,这是怎么回事?请帮忙
我已附上以下代码和错误日志
错误日志:
01-12 11:38:29.492: E/AndroidRuntime(20881): FATAL EXCEPTION: main
01-12 11:38:29.492: E/AndroidRuntime(20881): java.lang.IllegalStateException: GoogleApiClient must be connected.
01-12 11:38:29.492: E/AndroidRuntime(20881): at com.google.android.gms.common.internal.n.a(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881): at com.google.android.gms.internal.no.a(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881): at com.google.android.gms.internal.no.clearDefaultAccount(Unknown Source)
01-12 11:38:29.492: E/AndroidRuntime(20881): at com.dapoaugury.channelappdemo.ChannelAppMainActivity$DrawerItemClickListener.onItemClick(ChannelAppMainActivity.java:409)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.widget.AbsListView.performItemClick(AbsListView.java:1107)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2756)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.widget.AbsListView$1.run(AbsListView.java:3430)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.os.Handler.handleCallback(Handler.java:725)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.os.Looper.loop(Looper.java:137)
01-12 11:38:29.492: E/AndroidRuntime(20881): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-12 11:38:29.492: E/AndroidRuntime(20881): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 11:38:29.492: E/AndroidRuntime(20881): at java.lang.reflect.Method.invoke(Method.java:511)
01-12 11:38:29.492: E/AndroidRuntime(20881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 11:38:29.492: E/AndroidRuntime(20881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 11:38:29.492: E/AndroidRuntime(20881): at dalvik.system.NativeStart.main(Native Method)
代码:
//注销:
案例 2:
//DAPO:DEV02-20141231: 登录/注销选项交替,用户登录时登录更改为注销,反之亦然
if (isLogin.equals("Login")){
//If tab is login, user has not logged in, will navigate user to the login page and allow user to do a Google Login
Intent intent = new Intent(getApplicationContext(),
ChannelAppLoginInfoMainActivity.class);
startActivity(intent);
}if (isLogin.equals("Logout")){
//DAPO:DEV02:20150107:if tab is logout, will navigate user back to home page after user has logged out of Google account.
Toast.makeText(getApplicationContext(), "Logging out of ChannelApp!", Toast.LENGTH_LONG).show();
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
Intent intent= new Intent(getApplicationContext(),
ChannelAppMainActivity.class);
startActivity(intent);
}
break;
编辑代码:
//DAPO:DEV02-20150108: Declare Google variable:Google+client
private GoogleApiClient mGoogleApiClient;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//DAPO:DEV02-20150107: Initialize GoogleApiClient variable
mGoogleApiClient= new GoogleApiClient.Builder(this).addApi(Plus.API).
addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
//DAPO:DEV02-20150110: Invoking of GoogleApiClient and connecting GoogleApiClient
@Override
protected void onStart() {
super.onStart();
// Connect To Drive and Google+
mGoogleApiClient.connect();
}
@Override
protected void onStop(){
super.onStop();
// Disconnect from Drive and Google+
mGoogleApiClient.disconnect();
}
protected void onConnected(Bundle ConnectionHint){
//All Clients are connected
Intent intent = new Intent(getApplicationContext(),
ChannelAppAbstractGetNameTask.class);
startActivity(intent);
}
//DAPO:DEV02-20150110: End of Edited Version of Invoking of GoogleApiClient and connecting GoogleApiClient
最佳答案
else{
mPlusClient.clearDefaultAccount();
mPlusClient.disconnect();
mPlusClient.connect();
Intent intent= new Intent(getApplicationContext(),
ChannelAppMainActivity.class);
startActivity(intent);
}
改变它
else{
mPlusClient.clearDefaultAccount();
mPlusClient.disconnect();
mPlusClient.connect();
Intent intent = getIntent();
finish();
startActivity(intent);
}
关于android - 注销 Google 帐户时出现 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27852933/