Home » Android » GoogleApiClient is abstract; cannot be instantiated

GoogleApiClient is abstract; cannot be instantiated

Posted by: admin November 1, 2017 Leave a comment

Questions:

This is the code I have for trying to instantiate it:

private synchronized void callConnection(){
    mGoogleApiClient = new GoogleApiClient().Builder
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

This is what I have in my build.gradle:

compile 'com.google.android.gms:play-services:6.5.87'

I get this error:

Error:(222, 28) error: GoogleApiClient is abstract; cannot be instantiated
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
 >Compilation failed; see the compiler error output for details.
Answers:

It’s a builder pattern so you need to instantiate Builder ( static inner class) object which is responsible to construct the underlying object with the defined operations (invoked methods) so use

new GoogleApiClient.Builder()

instead of

new GoogleApiClient().Builder