为 Android 开发实现 GoogleApiClient Builder 时出错

Posted

技术标签:

【中文标题】为 Android 开发实现 GoogleApiClient Builder 时出错【英文标题】:Error implementing GoogleApiClient Builder for Android development 【发布时间】:2014-07-08 05:58:43 【问题描述】:

我正在按照 Google 的文档在应用中实现 Google+ 登录功能。

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

我按照指南执行了每个步骤,但陷入了由 GoogleApiClient.Builder 生成的错误,我彻底搜索但没有得到结果。 请帮我整理一下。谢谢。

错误代码:

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

错误信息:

   The method addConnectionCallbacks(GoogleApiClient.ConnectionCallbacks) in the type 
   GoogleApiClient.Builder is not applicable for the arguments (MainActivity)

完整的 MainActivity.java 代码:

    package mad.project.mightysatta;

    import android.content.Intent;
    import android.content.IntentSender.SendIntentException;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;

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

    public class MainActivity extends ActionBarActivity 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();

    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) 
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    



@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);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
    // return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
        return true;
    
    return super.onOptionsItemSelected(item);


/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment 

    public PlaceholderFragment() 
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    


@Override
public void onConnectionFailed(ConnectionResult result) 
    // TODO Auto-generated method stub

    if (!mIntentInProgress && result.hasResolution()) 
        try 
            mIntentInProgress = true;
            result.startResolutionForResult(this, // your activity
                    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();
        
    



@Override
public void onConnected(Bundle connectionHint) 
    // TODO Auto-generated method stub



@Override
public void onDisconnected() 
    // TODO Auto-generated method stub



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


protected void onStop() 
    super.onStop();

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


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

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


public void onConnectionSuspended(int cause) 
    mGoogleApiClient.connect();


    

在这段代码中,如果我注释掉 .addConnectionCallbacks 和 .addOnConnectionFailedListener ,那么错误就会消失。该错误似乎与他们的论点有关。

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

更新了 Main Activity ,将工具替换为

    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener

MainActivity.java(更新)

    package mad.project.mightysatta;

    import android.content.Intent;
    import android.content.IntentSender.SendIntentException;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;

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

    public class MainActivity extends ActionBarActivity implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.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();

    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) 
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    



@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);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
    // return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
        return true;
    
    return super.onOptionsItemSelected(item);


/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment 

    public PlaceholderFragment() 
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    


@Override
public void onConnectionFailed(ConnectionResult result) 
    // TODO Auto-generated method stub

    if (!mIntentInProgress && result.hasResolution()) 
        try 
            mIntentInProgress = true;
            result.startResolutionForResult(this, // your activity
                    RC_SIGN_IN);
         catch (SendIntentException e) 
            // The intent was canceled before it was sent. Return to
            // default
            // state and attempt to connect to get an updated
            // ConnectionResult.
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        
    



@Override
public void onConnected(Bundle connectionHint) 
    // TODO Auto-generated method stub



@Override
public void onDisconnected() 
    // TODO Auto-generated method stub



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


protected void onStop() 
    super.onStop();

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


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

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


public void onConnectionSuspended(int cause) 
    mGoogleApiClient.connect();


    

【问题讨论】:

Taha,我遇到了同样的问题,并想出了与您上面提到的相同的解决方案。你在使用 Android Studio 吗?如果是,您能否成功登录 G+? 我使用的是 ADT-Bundle,是的,它确实登录了。我认为它需要安装 G+ 应用程序并登录到您的应用程序的设备 【参考方案1】:

我在他们的文档中看到他们明确指示您包含此导入:

import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;

但是,抛出的错误期望与GooglePlayServicesClient.ConnectionCallbacks 不同的类,它要求GoogleApiClient.ConnectionCallbacks。尝试更改您的工具以使用更合格的类名。这似乎是唯一可能将代码抛出循环并且没有显式限定类名的情况,它将默认为直接导入的类名。

当您不得不质疑手册时,总是更难。

编辑:我的意思是这样的改变:

public class MainActivity 
    extends ActionBarActivity 
    implements GoogleApiClient.ConnectionCallbacks,
               GoogleApiClient.OnConnectionFailedListener 

【讨论】:

这是正确的。归档的文档错误。谢谢。 感谢您的修复。你会认为谷歌会测试他们自己的代码。实现 google play 服务的每一步都需要研究堆栈溢出来修复奇怪的错误。我希望有一天能完全实现它。 Michael,很高兴我不是唯一一个被文档绊倒的人。他们几乎没有任何样本在没有大量修复的情况下运行。 @Gerard 很高兴我能帮上忙。这绝对是具有挑战性的。 作为更新,我也遇到了这个问题。仍未修复 - 提交了另一个文档错误。【参考方案2】:

我也遇到了同样的问题,我通过以下方式解决了。

导入正确的 ConnectionCallbacks。

这是我的代码:

import android.content.Context;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;

public class GplusLogin implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener 
GoogleApiClient mGoogleApiClient;
GplusLogin(Context context)

    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) context)
            .addOnConnectionFailedListener(this)
            .build();


@Override
public void onConnected(Bundle bundle) 



@Override
public void onConnectionFailed(ConnectionResult connectionResult) 



@Override
public void onConnectionSuspended(int i) 



【讨论】:

谢谢,我遇到了同样的问题。我只导入了一个(正确的)ConnectionCallbacks,但它只是在 .addConnectionCallbacks 处崩溃,直到我将上下文显式转换为(GoogleApiClient.ConnectionCallbacks)。我在服务而不是活动中使用它,但它们都扩展了上下文,所以不确定它为什么重要。【参考方案3】:

事实证明,他们(谷歌)自己并没有遵循太多的指南。使用他们的Google Drive Android Quickstart 作为参考,他们实际上在教程的顶部提到了这一点,但他们给它起了误导性的名称“Android 快速入门”,即使它是特定于 Google Drive 的

the guide 顶部的注释:

【讨论】:

【参考方案4】:

点击alt enter并强制android studio实现:

GoogleApiClient.ConnectionCallbacks,

GoogleApiClient.OnConnectionFailedListener

【讨论】:

【参考方案5】:
   implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,OnMapReadyCallback

到活动

【讨论】:

以上是关于为 Android 开发实现 GoogleApiClient Builder 时出错的主要内容,如果未能解决你的问题,请参考以下文章

android 开发 将view保存为image的实现及将html保存为pdf格式

Android蓝牙开发——实现蓝牙聊天

android开发实现微信三方登录

Android 开发 如何实现高质量的录音

android 开发: 怎么把一个linearlayout设置为不可点击??

android开发 如何实现扫描本地二维码图片