wake-up-neo.com

Fehler beim Implementieren von GoogleApiClient Builder für Android Entwicklung

Ich verfolge die Dokumentation von Google, um die Google+ Anmeldefunktion in einer App zu implementieren.

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

Ich habe jeden Schritt gemäß der Anleitung befolgt, bin jedoch in einem Fehler stecken geblieben, der vom GoogleApiClient.Builder generiert wurde. Ich habe gründlich gesucht, aber kein Ergebnis erzielt. Bitte helfen Sie mir, das zu klären. Vielen Dank.

Fehlercode:

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

Fehlermeldung :

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

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

    }

Wenn ich in diesem Code .addConnectionCallbacks und .addOnConnectionFailedListener auskommentiere, verschwindet der Fehler. Der Fehler scheint mit ihren Argumenten zu zusammenhängen.

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

Aktualisierte Hauptaktivität nach dem Ersetzen von Anbaugeräten auf

    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener

MainActivity.Java (aktualisiert)

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

    }
34
Taha Rushain

Ich sehe in ihrer Dokumentation, wo sie Sie eindeutig anweisen, diesen Import einzuschließen:

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

Der Fehler, der ausgelöst wird, erwartet jedoch eine andere Klasse als GooglePlayServicesClient.ConnectionCallbacks, es fragt nach GoogleApiClient.ConnectionCallbacks. Versuchen Sie, Ihre Anbaugeräte so zu ändern, dass der Name der besser qualifizierten Klasse verwendet wird. Dies scheint die einzig mögliche Möglichkeit zu sein, den Code für eine Schleife auszulösen. Ohne den explizit qualifizierten Klassennamen wird standardmäßig der direkt importierte Klassenname verwendet.

Es ist immer schwieriger, das Handbuch in Frage zu stellen.

Edit: Ich meine eine Änderung wie folgt:

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

Ich stand auch vor dem gleichen Problem, ich löste es, indem ich die folgenden Dinge tat.

importieren Sie die richtigen ConnectionCallbacks.

hier ist mein Code:

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) {

}
}
16
raju

es stellt sich heraus, dass sie (Google) nicht viel von der Anleitung selbst folgen. Verwenden Sie ihr Google Drive Android Quickstart als Referenz, die sie tatsächlich oben im Tutorial erwähnen, aber sie geben ihr sogar den irreführenden Namen "Android Quickstart" obwohl es spezifisch für Google Drive ist

Die Notiz oben in der AnleitungThe note at the top of the guide

2
woojoo666

klicke Alt Enter und erzwinge Android studio um zu implementieren:

GoogleApiClient.ConnectionCallbacks,

GoogleApiClient.OnConnectionFailedListener

2
carlos
   implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,OnMapReadyCallback

zur Aktivität

0
Brinda Rathod