hiccup
hiccup

Reputation: 42

error in com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks

I am new to Android, currently I am working on a very basic Google map. and getting this error. Could anybody please help me out on this?

Here is the error in logcat

`07-02 10:49:20.882  25215-25215/com.example.prashant.nuhani_go      E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.prashant.nuhani_go, PID: 25215
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.prashant.nuhani_go/com.example.prashant.nuhani_go.seco ndLocationChoosen}:     java.lang.ClassCastException:  com.example.prashant.nuhani_go.secondLocationChoosen cannot be cast to   com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
            at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassCastException:     com.example.prashant.nuhani_go.secondLocationChoosen cannot be cast to com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
        at com.example.prashant.nuhani_go.secondLocationChoosen.onCreate(secondLocationChoosen.java:46)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278).....

        

my java file :

      package com.example.prashant.nuhani_go;
      import android.support.v7.app.ActionBarActivity;
      import android.os.Bundle;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.view.View;
      import android.widget.AutoCompleteTextView;
      import android.widget.Button;
      import android.widget.EditText;
      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.location.places.Places;


     public class secondLocationChoosen extends ActionBarActivity {
private GoogleApiClient mGoogleApiClient;
private AutoCompleteTextView mAutocompleteView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second_location_choosen);

  // Checking the destination and starting location, they shouldn't be empty
   final EditText editText3 =(EditText)findViewById(R.id.editText3);
   final EditText editText4 =(EditText)findViewById(R.id.editText4);
        Button button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if( editText3.getText().toString().length() == 0 )
                editText3.setError( "This field cannot be empty !" );
            if( editText4.getText().toString().length() == 0 )
                editText4.setError( "This field cannot be empty !" );
        }
    });


    // Construct a GoogleApiClient for the {@link Places#GEO_DATA_API} using AutoManage
    // functionality, which automatically sets up the API client to handle Activity lifecycle
    // events. If your activity does not extend FragmentActivity, make sure to call connect()
    // and disconnect() explicitly.
    mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks((ConnectionCallbacks) this)
            .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
            .build();


    // Retrieve the AutoCompleteTextView that will display Place suggestions.
    //mAutocompleteView = (AutoCompleteTextView)
    //        findViewById(R.id.place);
    // Register a listener that receives callbacks when a suggestion has been selected
    //mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_second_location_choosen, menu);
    return true;
}

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

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}
@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

I have enabled google maps api, and I have also added my key in the manifest file.

Upvotes: 0

Views: 2147

Answers (2)

Kartik Gogia
Kartik Gogia

Reputation: 25

Make your activity implement GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener

Upvotes: 2

andrewdleach
andrewdleach

Reputation: 2466

Remove the casts in your GoogleAPIClient instantiation and implement the methods in your class.

Try implementing those interfaces in the class and you'll see what you're missing. Android Studio will throw you a couple "does not implement x method" warnings

You are telling the ApiClient that "this" current instance of your class implements those two interfaces, but doesn't actually implement them. Therefore the ClassCastException.

Upvotes: 0

Related Questions