2014-02-22 72 views
1

我一直在試圖弄清楚如何解決這個問題很長一段時間,但我找不到任何錯誤信息。startResolutionForResult返回一個錯誤(應用程序崩潰),GoogleApiClient連接

代碼來源於:http://developer.android.com/google/auth/api-client.html

首先,我建立GoogleApiClient對象,然後在onStart()嘗試連接它。 連接失敗,我結束了內部「onConnectionFailed()」 ..

在這裏,我打印的結果,這就是我得到:「SIGN_IN_REQUIRED」

由於這個錯誤有一個解決方案「的結果。 startResolutionForResult(this,REQUEST_RESOLVE_ERROR)「將被調用。

這裏是程序突然崩潰的地方。

任何幫助將appriciated我對android開發很新,所以容易對我!

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.IntentSender.SendIntentException; 
import android.util.Log; 
import android.view.Menu; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.plus.Plus; 

public class MainActivity extends Activity implements 
ConnectionCallbacks, OnConnectionFailedListener { 

    /* Client used to interact with Google APIs. */ 
    private GoogleApiClient mGoogleApiClient; 
    // Request code to use when launching the resolution activity 
    private static final int REQUEST_RESOLVE_ERROR = 1001; 
    // Bool to track whether the app is already resolving an error 
    private boolean mResolvingError = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     super.onCreate(savedInstanceState); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(Plus.API, null) 
       .addScope(Plus.SCOPE_PLUS_LOGIN) 
       .build(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 
    protected void onStop() { 
     super.onStop(); 

     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     if (mResolvingError) { 
      // Already attempting to resolve an error. 
      return; 
     } else if (result.hasResolution()) { 
      Log.d("MyApp",result.toString()); 
      try { 
       mResolvingError = true; 
       result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); 
      } catch (SendIntentException e) { 
       // There was an error with the resolution intent. Try again. 
       mGoogleApiClient.connect(); 
      } 
     } else { 
      // Show dialog using GooglePlayServicesUtil.getErrorDialog() 
      //showErrorDialog(result.getErrorCode()); 
      mResolvingError = true; 
     } 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 

    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_RESOLVE_ERROR) { 
      mResolvingError = false; 
      if (resultCode == RESULT_OK) { 
       // Make sure the app is not already connected or attempting to connect 
       if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) { 
        mGoogleApiClient.connect(); 
       } 
      } 
     } 
    } 
} 
+1

能否打印從'ADB logcat'堆棧跟蹤? – Hounshell

+1

我遇到同樣的問題。在onConnectionFailed()方法中獲取ConnectionResult = null。 –

+0

PS:您不需要在onConnectionSuspended方法中再次調用connect(),因爲它會自動嘗試重新連接,並在成功時調用onConnected()。 – trinity420

回答

1

onCreate方法嘗試:

  • 去除重複行super.onCreate(savedInstanceState);
  • 刪除.addScope(Plus.SCOPE_PLUS_LOGIN).setScopes("PLUS_LOGIN")因爲是在我的情況

如果這沒有幫助,嘗試從SDK示例導入並運行很好的示例項目:

<android-sdk-folder>/extras/google/google_play_services/ 

https://developers.google.com/+/mobile/android/getting-started

相關問題