[英]Google+ sign in button in android . These are my java file and error log
JAVA:
package com.example.api_test;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.plus.Plus;
public class MainActivity extends Activity implements OnClickListener,ConnectionCallbacks, OnConnectionFailedListener{
Button bSignIn;
private static final int RC_SIGN_IN=1;
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress, mSignInClicked;
private ConnectionResult mConnectionResult;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
bSignIn = (Button)findViewById(R.id.sign_in_button);
bSignIn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button
&& !mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
protected void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
private void resolveSignInError() {
if (mConnectionResult.hasResolution() && mGoogleApiClient != null) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
RC_SIGN_IN, null, 0, 0, 0);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
protected void onStop(){
super.onStop();
if (mGoogleApiClient.isConnected()){
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress && result.hasResolution()){
try{
mIntentInProgress = true;
startIntentSenderForResult(result.getResolution().getIntentSender(), RC_SIGN_IN ,null ,0 ,0 , 0);
} catch (SendIntentException e){
mIntentInProgress =false;
mGoogleApiClient.connect();
}
}
if (mSignInClicked){
resolveSignInError();
}
}
@Override
public void onConnected(Bundle connectionHint) {
mSignInClicked = false;
Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionSuspended(int cause) {
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();
}
}
}
}
錯誤日志:
07-26 18:39:18.940: E/AndroidRuntime(888): FATAL EXCEPTION: main
07-26 18:39:18.940:E / AndroidRuntime(888):進程:com.example.api_test,PID:888 07-26 18:39:18.940:E / AndroidRuntime(888):java.lang.NullPointerException 07- 26 18:39:18.940:E / AndroidRuntime(888):在com.example.api_test.MainActivity.resolveSignInError(MainActivity.java:61)07-26 18:39:18.940:E / AndroidRuntime(888):在com。 example.api_test.MainActivity.onClick(MainActivity.java:50)07-26 18:39:18.940:E / AndroidRuntime(888):at android.view.View.performClick(View.java:4438)07-26 18: 39:18.940:E / AndroidRuntime(888):在android.view.View $ PerformClick.run(View.java:18422)07-26 18:39:18.940:E / AndroidRuntime(888):在android.os.Handler .handleCallback(Handler.java:733)07-26 18:39:18.940:E / AndroidRuntime(888):at android.os.Handler.dispatchMessage(Handler.java:95)07-26 18:39:18.940:E / AndroidRuntime(888):位於android.os.Looper.loop(Looper.java:136)07-26 18:39:18.940:E / AndroidRuntime(888):位於android.app.ActivityThread.main(ActivityThread.java: 5017)07-26 18:39:18.940:E / AndroidRuntim e(888):at java.lang.reflect.Method.invokeNative(本機方法)07-26 18:39:18.940:E / AndroidRuntime(888):at java.lang.reflect.Method.invoke(Method.java: 515)07-26 18:39:18.940:E / AndroidRuntime(888):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)07-26 18:39:18.940:E / AndroidRuntime(888):位於com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)07-26 18:39:18.940:E / AndroidRuntime(888):位於dalvik.system.NativeStart.main(Native方法)
似乎您尚未將變量MConnectionResult
初始化為任何東西,並且在第61行上您收到了NullPointer異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.