整合谷歌加android错误

时间:2014-06-30 01:45:01

标签: android oauth

https://developers.google.com/+/mobile/android/getting-started 这是我的代码:

public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener{
 private static final int RC_SIGN_IN = 0;

  /* Client used to interact with Google APIs. */
  private GoogleApiClient mGoogleApiClient;
  /* A flag indicating that a PendingIntent is in progress and prevents
   * us from starting further intents.
   */
  private boolean mIntentInProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, null).addScope(Plus.SCOPE_PLUS_LOGIN).build();
    setContentView(R.layout.activity_main);
}
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
  }

  protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
      mGoogleApiClient.disconnect();
    }
  }
  public void onConnectionFailed(ConnectionResult result) {
      if (!mIntentInProgress && result.hasResolution()) {
        try {
          mIntentInProgress = true;
          result.startResolutionForResult(this, RC_SIGN_IN);
        } 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();
        }
      }
    }

    public void onConnected(Bundle connectionHint) {
      // We've resolved any connection errors.  mGoogleApiClient can be used to
      // access Google APIs on behalf of the user.
    }
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
          if (requestCode == RC_SIGN_IN) {
            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
              mGoogleApiClient.connect();
            }
          }
        }
    @Override
    public void onConnectionSuspended(int arg0) {
        mGoogleApiClient.connect();
    }
}.

首先,我看到行mIntentInProgress == false;并收到错误:
Syntax error on token "==", invalid AssignmentOperator所以我改为mIntentInProgress = false;然后,当我跑步时,它崩溃了 Logcat:.......................

06-30 01:33:49.186: E/AndroidRuntime(2776): FATAL EXCEPTION: main    
06-30 01:33:49.186: E/AndroidRuntime(2776): Process: com.example.oauth, PID: 2776    
06-30 01:33:49.186: E/AndroidRuntime(2776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oauth/com.example.oauth.MainActivity}: java.lang.NullPointerException: Null options are not permitted for this Api
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.ActivityThread.access$800(ActivityThread.java:135)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.os.Handler.dispatchMessage(Handler.java:102)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.os.Looper.loop(Looper.java:136)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.ActivityThread.main(ActivityThread.java:5017)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at java.lang.reflect.Method.invokeNative(Native Method)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at java.lang.reflect.Method.invoke(Method.java:515)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at dalvik.system.NativeStart.main(Native Method)    
06-30 01:33:49.186: E/AndroidRuntime(2776): Caused by: java.lang.NullPointerException: Null options are not permitted for this Api    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at com.google.android.gms.internal.fq.b(Unknown Source)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at com.google.android.gms.common.api.GoogleApiClient$Builder.addApi(Unknown Source)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at com.example.oauth.MainActivity.onCreate(MainActivity.java:28)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.Activity.performCreate(Activity.java:5231)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)    
06-30 01:33:49.186: E/AndroidRuntime(2776):     ... 11 more

2 个答案:

答案 0 :(得分:9)

如错误中所述 - 您不能使用null PlusOptions - 连接到Plus API应如下所示:

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this).addOnConnectionFailedListener(this)
    .addApi(Plus.API, new Plus.PlusOptions.Builder().build()) // note the options
    .addScope(Plus.SCOPE_PLUS_LOGIN).build();

答案 1 :(得分:0)

而不是

.addApi(Plus.API, null)

试试这个:

.addApi(Plus.API)

这对我有用。