Android 注销Google帐户时出现空指针异常
已创建Google帐户登录/注销方法。主要思想是当用户点击 “使用谷歌登录”,它将引导用户进入用户配置文件页面,当用户决定登录时,将调用谷歌注销方法并将用户重新引导回主页。但是,问题是,当用户单击注销按钮时,会发生以下错误,发生了什么?请帮忙 我已附上以下代码和错误日志 错误日志: 代码: //注销:Android 注销Google帐户时出现空指针异常,android,google-api,google-plus,google-api-client,google-login,Android,Google Api,Google Plus,Google Api Client,Google Login,已创建Google帐户登录/注销方法。主要思想是当用户点击 “使用谷歌登录”,它将引导用户进入用户配置文件页面,当用户决定登录时,将调用谷歌注销方法并将用户重新引导回主页。但是,问题是,当用户单击注销按钮时,会发生以下错误,发生了什么?请帮忙 我已附上以下代码和错误日志 错误日志: 代码: //注销: 案例2: //DAPO:DEV02-20141231:交替登录/注销选项,当用户登录时,登录更改为注销,反之亦然 if (isLogin.equals("Login")){
案例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 = getIntent();
finish();
startActivity(intent);
}
设法排除故障并发现困扰项目的问题: 首次发行:
Plus.AccountApi.clearDefaultAccount(mgoogleapClient)由于用户首次访问应用程序时没有默认帐户,因此退出方法中的代码>
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+
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
protected void onConnected(Bundle ConnectionHint){
//All Clients are connected
mSignInClicked=false;
Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}
/* A helper method to resolve the current ConnectionResult error. */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
RC_SIGN_IN, null, 0, 0, 0);
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
//DAPO:DEV02-20150110: End of Edited Version of Invoking of GoogleApiClient and connecting GoogleApiClient
//Logout:
case 2:
//DAPO:DEV02-20141231: alternation of login/logout options, login to change to logout when user is login and vice versa
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();
//To disconnect user from GoogleApiClient server, remove all Google login credentials.
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
//Return user to Main HomePage
startActivity(new Intent(getApplicationContext(), ChannelAppMainActivity.class));
// make sure the user can not access the page after he/she is logged out
// clear the activity stack
finish();
}
break;
//DAPO:DEV02-20141231: End of Edited Version to implement GoogleApiClient logout method and implementation for login/logout method.
哪一行是ChannelAppMainActivity.java中的第377行?@Rohit5k2是这一行“mpluClient.clearDefaultAccount();”哦……
mpluClient
为空。请发布ChannelAppMainActivity
activity@Rohit5k2,ChannelAppMainActivity活动的代码是什么?以上代码是ChannelAppMainActivity的一部分。我已经初始化了MPlucClient,但没有查看代码,我无法告诉您为什么为空,这就是为什么我需要完成该活动的代码。您加上客户端对象不知何故变为null。您如何解释此代码?当您未声明意图路径时,意图是什么?您正在重新创建您的活动,以便在注销后重新创建,并使用登录按钮返回同一活动尝试解决方案后仍然存在null指针错误不启动活动检查witout启动活动是否注销?如何初始化MPluClient变量?显然,这是不正确的
else{
mPlusClient.clearDefaultAccount();
mPlusClient.disconnect();
mPlusClient.connect();
Intent intent = getIntent();
finish();
startActivity(intent);
}
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+
if(mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
protected void onConnected(Bundle ConnectionHint){
//All Clients are connected
mSignInClicked=false;
Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}
/* A helper method to resolve the current ConnectionResult error. */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
RC_SIGN_IN, null, 0, 0, 0);
} catch (SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
//DAPO:DEV02-20150110: End of Edited Version of Invoking of GoogleApiClient and connecting GoogleApiClient
//Logout:
case 2:
//DAPO:DEV02-20141231: alternation of login/logout options, login to change to logout when user is login and vice versa
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();
//To disconnect user from GoogleApiClient server, remove all Google login credentials.
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
//Return user to Main HomePage
startActivity(new Intent(getApplicationContext(), ChannelAppMainActivity.class));
// make sure the user can not access the page after he/she is logged out
// clear the activity stack
finish();
}
break;
//DAPO:DEV02-20141231: End of Edited Version to implement GoogleApiClient logout method and implementation for login/logout method.