Android 集成g+时出现空指针异常;签名
这是我的密码Android 集成g+时出现空指针异常;签名,android,google-plus,google-signin,Android,Google Plus,Google Signin,这是我的密码 public class gsign extends Activity implements ConnectionCallbacks,OnConnectionFailedListener { private static final int RC_SIGN_IN = 0; private static final String TAG = "MainActivity"; private GoogleApiClient mGoogleApiClient; private boolea
public class gsign extends Activity implements ConnectionCallbacks,OnConnectionFailedListener {
private static final int RC_SIGN_IN = 0;
private static final String TAG = "MainActivity";
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
protected void onStart() {
super.onStart();
signInWithGplus();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
resolveSignInError();
}
}
}
@Override
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();
}
}
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
// Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
getProfileInformation();
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
else
getProfileInformation();
}
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
}
我也有代码在我的云后端注册用户,并获取他们的信息,但我得到以下错误
Caused by: java.lang.NullPointerException
at com.example.nirmal.loginpage.gsign.resolveSignInError(gsign.java:127)
at com.example.nirmal.loginpage.gsign.signInWithGplus(gsign.java:117)
at com.example.nirmal.loginpage.gsign.onCreate(gsign.java:56)
以下错误指向if(mcConnectionResult.hasResolution())
通常,从onCreate()
方法调用signInWithGPlus()
时可能会导致此错误,但即使从onStart()
调用后,我也会遇到相同的错误。
问题是我在另一个应用程序中使用了相同的代码(事实上,我是从那个应用程序中复制的),它运行得很好,所以我不知道是什么导致了这个错误
我还为此应用程序创建了单独的OAuth客户端密钥。那么有人能告诉我哪里出了问题吗???堆栈跟踪说明了一切 只有在
onStart()
之前,您才能调用mgoogleapclient.connect()
。同时回到onCreate()
,在onStart()
之前调用,调用符号with gplus()代码>方法
!该方法中的mGoogleApiClient.isConnecting()
为true,导致调用ResolveSignError()
您尚未为mConnectionResult
赋值,因此在resolvesignError()
中调用mConnectionResult.hasResolution()
将抛出NullPointerException
可能的重复项。我还尝试从onStart()调用了带gplus的SigninWithSign。然后我也遇到了同样的错误。它必须在调用mgoogleapclient.connect()
之后执行,这样当您到达方法时,mgoogleapclient.isConnecting()
将为真。查看代码,您确定需要调用signInWithGplus()代码>是吗?如果没有它,则在到达onConnected(…)
回调后调用mgoogleapclient.connect()
后,它看起来就像是调用getProfileInformation()
无论如何都会被调用,因此我会先尝试删除signInWithGplus()
。在以前的应用程序中,我从onClicklistener调用了signInWithGplus()
,但由于我在这里进行显式调用,因此我认为最好删除该函数