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();
}
}
}
}
Error log:
07-26 18:39:18.940: E/AndroidRuntime(888): FATAL EXCEPTION: main
07-26 18:39:18.940: E/AndroidRuntime(888): Process: 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): at com.example.api_test.MainActivity.resolveSignInError(MainActivity.java:61) 07-26 18:39:18.940: E/AndroidRuntime(888): at 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): at android.view.View$PerformClick.run(View.java:18422) 07-26 18:39:18.940: E/AndroidRuntime(888): at 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): at android.os.Looper.loop(Looper.java:136) 07-26 18:39:18.940: E/AndroidRuntime(888): at android.app.ActivityThread.main(ActivityThread.java:5017) 07-26 18:39:18.940: E/AndroidRuntim e(888): at java.lang.reflect.Method.invokeNative(Native Method) 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): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 07-26 18:39:18.940: E/AndroidRuntime(888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 07-26 18:39:18.940: E/AndroidRuntime(888): at dalvik.system.NativeStart.main(Native Method)
似乎您尚未将变量MConnectionResult
初始化为任何东西,并且在第61行上您收到了NullPointer异常。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.