错误实施GoogleApiClient生成器为Android开发生成器、错误、GoogleApiClient、Android

2023-09-13 00:23:25 作者:励志的

我下面谷歌的文档来实现的Google+登录功能到应用程序。

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

我根据向导跟着每一步,但被困在由GoogleApiClient.Builder产生的错误,我彻底搜查了一遍,但没有得到结果。 请帮我整理出来。谢谢你。

错误code:

  mGoogleApiClient =新GoogleApiClient.Builder(本)
            .addConnectionCallbacks(本)
            .addOnConnectionFailedListener(本).addApi(Plus.API,空)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();
 

错误消息:

 的方法addConnectionCallbacks(GoogleApiClient.ConnectionCallbacks)在类型
   GoogleApiClient.Builder是不适用的参数(MainActivity)
 

完整MainActivity.java code:

 包mad.project.mightysatta;

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

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

    公共类MainActivity扩展ActionBarActivity工具
    ConnectionCallbacks,OnConnectionFailedListener {

/ *请求code用来在用户交互调用的迹象。 * /
私有静态最终诠释RC_SIGN_IN = 0;

/ *客户端用来与谷歌API的交互。 * /
私人GoogleApiClient mGoogleApiClient;

/ *
 *一个标志,指示一个PendingIntent正在进行和prevents我们
 *启动进一步的意图。
 * /
私人布尔mIntentInProgress;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    mGoogleApiClient =新GoogleApiClient.Builder(本)
            .addConnectionCallbacks(本)
            .addOnConnectionFailedListener(本).addApi(Plus.API,空)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    的setContentView(R.layout.activity_main);

    如果(savedInstanceState == NULL){
        getSupportFragmentManager()的BeginTransaction()
                。新增(R.id.container,新PlaceholderFragment())提交()。
    }

}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){

    //充气菜单;这增加了项目操作栏,如果它是present。
    // getMenuInflater()膨胀(R.menu.main,菜单);
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions,菜单);
    返回super.onCreateOptionsMenu(菜单);
    //返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。将操作栏
    //自动在主/向上按钮操作的点击,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT的id = item.getItemId();
    如果(ID == R.id.action_settings){
        返回true;
    }
    返回super.onOptionsItemSelected(项目);
}

/ **
 *包含一个简单的视图的占位符片段。
 * /
公共静态类PlaceholderFragment扩展片段{

    公共PlaceholderFragment(){
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,
                假);
        返回rootView;
    }
}

@覆盖
公共无效onConnectionFailed(ConnectionResult结果){
    // TODO自动生成方法存根

    如果(mIntentInProgress&安培;!&安培; result.hasResolution()){
        尝试 {
            mIntentInProgress =真;
            result.startResolutionForResult(这//你的活动
                    RC_SIGN_IN);
        }赶上(SendIntentException E){
            //它被发送之前做的目的被取消。返回到
            // 默认
            //状态,并尝试连接以获取更新
            // ConnectionResult。
            mIntentInProgress = FALSE;
            mGoogleApiClient.connect();
        }
    }

}

@覆盖
公共无效onConnected(包connectionHint){
    // TODO自动生成方法存根

}

@覆盖
公共无效onDisconnected(){
    // TODO自动生成方法存根

}

保护无效的OnStart(){
    super.onStart();
    mGoogleApiClient.connect();
}

保护无效的onStop(){
    super.onStop();

    如果(mGoogleApiClient.isConnected()){
        mGoogleApiClient.disconnect();
    }
}

保护无效onActivityResult(INT申请code,INT响应code,
        意向意图){
    如果(要求code == RC_SIGN_IN){
        mIntentInProgress = FALSE;

        如果(!mGoogleApiClient.isConnecting()){
            mGoogleApiClient.connect();
        }
    }
}

公共无效onConnectionSuspended(INT原因){
    mGoogleApiClient.connect();
}

    }
 
基于WanAndroid开放API实现的文章阅读APP

在此code,如果我注释掉.addConnectionCallbacks和.addOnConnectionFailedListener,然后将错误消失。该错误似乎与他们的论据是相关的。

  mGoogleApiClient =新GoogleApiClient.Builder(本)
        // .addConnectionCallbacks(本)
        // .addOnConnectionFailedListener(本)
            .addApi(Plus.API,空)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();
 

更新的主要活动,更换工具后

  GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener
 

MainActivity.java(更新)

 包mad.project.mightysatta;

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

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

    公共类MainActivity扩展ActionBarActivity工具
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {

/ *请求code用来在用户交互调用的迹象。 * /
私有静态最终诠释RC_SIGN_IN = 0;

/ *客户端用来与谷歌API的交互。 * /
私人GoogleApiClient mGoogleApiClient;

/ *
 *一个标志,指示一个PendingIntent正在进行和prevents我们
 *启动进一步的意图。
 * /
私人布尔mIntentInProgress;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    mGoogleApiClient =新GoogleApiClient.Builder(本)
            .addConnectionCallbacks(本)
            .addOnConnectionFailedListener(本)
            .addApi(Plus.API,空)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    的setContentView(R.layout.activity_main);

    如果(savedInstanceState == NULL){
        getSupportFragmentManager()的BeginTransaction()
                。新增(R.id.container,新PlaceholderFragment())提交()。
    }

}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){

    //充气菜单;这增加了项目操作栏,如果它是present。
    // getMenuInflater()膨胀(R.menu.main,菜单);
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions,菜单);
    返回super.onCreateOptionsMenu(菜单);
    //返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。将操作栏
    //自动在主/向上按钮操作的点击,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT的id = item.getItemId();
    如果(ID == R.id.action_settings){
        返回true;
    }
    返回super.onOptionsItemSelected(项目);
}

/ **
 *包含一个简单的视图的占位符片段。
 * /
公共静态类PlaceholderFragment扩展片段{

    公共PlaceholderFragment(){
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,
                假);
        返回rootView;
    }
}

@覆盖
公共无效onConnectionFailed(ConnectionResult结果){
    // TODO自动生成方法存根

    如果(mIntentInProgress&安培;!&安培; result.hasResolution()){
        尝试 {
            mIntentInProgress =真;
            result.startResolutionForResult(这//你的活动
                    RC_SIGN_IN);
        }赶上(SendIntentException E){
            //它被发送之前做的目的被取消。还给
            // 默认
            //状态,并尝试连接以获取更新
            // ConnectionResult。
            mIntentInProgress = FALSE;
            mGoogleApiClient.connect();
        }
    }

}

@覆盖
公共无效onConnected(包connectionHint){
    // TODO自动生成方法存根

}

@覆盖
公共无效onDisconnected(){
    // TODO自动生成方法存根

}

保护无效的OnStart(){
    super.onStart();
    mGoogleApiClient.connect();
}

保护无效的onStop(){
    super.onStop();

    如果(mGoogleApiClient.isConnected()){
        mGoogleApiClient.disconnect();
    }
}

保护无效onActivityResult(INT申请code,INT响应code,
        意向意图){
    如果(要求code == RC_SIGN_IN){
        mIntentInProgress = FALSE;

        如果(!mGoogleApiClient.isConnecting()){
            mGoogleApiClient.connect();
        }
    }
}

公共无效onConnectionSuspended(INT原因){
    mGoogleApiClient.connect();
}

    }
 

解决方案

我在他们的文档,他们清楚地指导你,包括此导入见:

 进口com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
 

不过,被抛出的错误期待一个不同的类比 GooglePlayServicesClient.ConnectionCallbacks ,它要求 GoogleApiClient.ConnectionCallbacks 。试着改变你的工具使用更合格的类名称。这看起来是唯一可能的事情抛$ C $下一个循环,并没有明确的限定类名,将默认为直接导入的类名。

这是当你有质疑手动总是强硬。

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

 公共类MainActivity
    扩展ActionBarActivity
    实现GoogleApiClient.ConnectionCallbacks,
               GoogleApiClient.OnConnectionFailedListener {
 

I am following Google's documentation to implement Google+ Sign In feature into an app.

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

I followed each step according to the guide but got stuck in an error generated by the GoogleApiClient.Builder , I searched thoroughly but got no result. Please help me sort it out. Thank you.

Error code:

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

Error Message :

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

Complete MainActivity.java code :

    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();
}

    }

In this code if I comment out .addConnectionCallbacks and .addOnConnectionFailedListener , then the error goes away. The error seems to be related with their arguments.

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

Updated Main Activity , after replacing implements to

    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener

MainActivity.java(Updated)

    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();
}

    }

解决方案

I see in their documentation where they clearly instruct you to include this import:

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

However, the error being thrown is expecting a different class than GooglePlayServicesClient.ConnectionCallbacks, it's asking for GoogleApiClient.ConnectionCallbacks. Try changing your implements to use the more-qualified class name. That looks to be the only possible thing throwing the code for a loop and without the explicit qualified classname, it will default to the directly imported class name.

It's always tougher when you have to question the manual.

Edit: I mean a change like this:

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