Sahal Sajjad
Sahal Sajjad

Reputation: 235

Android Error : addConnectionCallbacks not applicable for arguments

Please see my MainActivity.java and tell me why i m getting the following error

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

package com.example.myapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import com.google.android.gms.common.api.GoogleApiClient;
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.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends Activity {

    private GoogleApiClient mGoogleApiClient ;   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        buildGoogleApiClient();

        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
    }
}

Please tell me why it is showing so. Do I have to add any imports ? I got this code from developer.android.com

Upvotes: 1

Views: 3249

Answers (1)

Mattia Maestrini
Mattia Maestrini

Reputation: 32790

Your MainActivity needs to implements the interfaces ConnectionCallbacks and OnConnectionFailedListener. After that you need to add the methods onConnected, onConnectionSuspended and onConnectionFailed:

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

    private GoogleApiClient mGoogleApiClient ;   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        buildGoogleApiClient();

        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
    }

    @Override
    public void onConnected(Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
}

Upvotes: 9

Related Questions