2014-02-13 120 views
25

我正在跟着教程here,並且在onConnectionFailed類中出現錯誤The method getIntentSender() is undefined for the type ConnectionResult在android中執行谷歌播放服務

全碼

package com.alfalfa.thisthat; 

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

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

public class LoginActivity extends Activity implements 
ConnectionCallbacks, OnConnectionFailedListener { 

    /* Request code used to invoke sign in user interactions. */ 
    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(); 
    } 

    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    protected void onStop() { 
     super.onStop(); 

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

    @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; 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
      if (!mIntentInProgress && result.hasResolution()) { 
      try { 
       mIntentInProgress = true; 
       startIntentSenderForResult(result.getIntentSender(), 
        RC_SIGN_IN, null, 0, 0, 0); 
      } 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(); 
      } 
      } 
     } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     // We've resolved any connection errors. mGoogleApiClient can be used to 
     // access Google APIs on behalf of the user. 
    } 

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

    protected void onActivityResult(int requestCode, int responseCode, Intent intent) { 
      if (requestCode == RC_SIGN_IN) { 
       mIntentInProgress = false; 

       if (!mGoogleApiClient.isConnecting()) { 
        mGoogleApiClient.connect(); 
       } 
      } 
    } 
} 

編輯: logcat的

02-13 19:12:31.763: E/AndroidRuntime(21257): FATAL EXCEPTION: main 
02-13 19:12:31.763: E/AndroidRuntime(21257): Process: com.alfalfa.thisthat, PID: 21257 
02-13 19:12:31.763: E/AndroidRuntime(21257): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alfalfa.thisthat/com.alfalfa.thisthat.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.alfalfa.thisthat/com.alfalfa.thisthat.LoginActivity}; have you declared this activity in your AndroidManifest.xml? 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.ActivityThread.access$800(ActivityThread.java:135) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.os.Handler.dispatchMessage(Handler.java:102) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.os.Looper.loop(Looper.java:136) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at java.lang.reflect.Method.invokeNative(Native Method) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at java.lang.reflect.Method.invoke(Method.java:515) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at dalvik.system.NativeStart.main(Native Method) 
02-13 19:12:31.763: E/AndroidRuntime(21257): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.alfalfa.thisthat/com.alfalfa.thisthat.LoginActivity}; have you declared this activity in your AndroidManifest.xml? 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Activity.startActivityForResult(Activity.java:3424) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Activity.startActivityForResult(Activity.java:3385) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Activity.startActivity(Activity.java:3627) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Activity.startActivity(Activity.java:3595) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at com.alfalfa.thisthat.MainActivity.onCreate(MainActivity.java:16) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Activity.performCreate(Activity.java:5231) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
02-13 19:12:31.763: E/AndroidRuntime(21257): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
02-13 19:12:31.763: E/AndroidRuntime(21257): ... 11 more 

回答

29

截至今天和谷歌播放SDK版本15的,這已經被替換

result.startResolutionForResult(this, // your activity 
           RC_SIGN_IN); 

根據更新的documentation

+0

如果我改變該行所涉及的LoginActivity類上面沒有顯示任何錯誤,但如果我從MainActivity到LoginActivity的意圖,該應用程序強制關閉運行程序。還有什麼我做錯了嗎?謝謝。 –

+0

發佈您的logcat,如果它強制關閉 – ianhanniballake

+0

我更新了問題與l –

5

希望它可以幫助你

result.getResolution().getIntentSender() 
+3

Hiya,這可能很好的解決了這個問題......但是如果你能夠提供一些關於它是如何工作和爲什麼工作的解釋的話,那就太好了:)不要忘記 - Stack堆棧中有一堆新手,他們可以從你的專業知識中學到一兩件事 - 對你來說顯而易見的事情對他們來說可能並非如此。 –

+0

這行代碼到底在哪裏? –