Manas Maity
Manas Maity

Reputation: 149

Non-static method isGooglePlayServicesAvailable and getErrorDialog cannot be referenced from a static context

I am writing this code and it shows the error non-static method GoogleApiAvailability.isGooglePlayServicesAvailable(Context context) and GoogleApiAvailability.getErrorDialog (Activity activity, int errorCode, int requestCode) cannot be referenced from a static context.

package com.xamarin.gcmexample;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.GoogleApiAvailability;
import android.util.Log;
import android.widget.TextView;

public class Main extends AppCompatActivity {
    TextView msgText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        msgText = (TextView) findViewById(R.id.msgText);

    }
    protected void IsPlayServicesAvailable ()
    {
        int resultCode = GoogleApiAvailability.isGooglePlayServicesAvailable(this);
        if (resultCode == ConnectionResult.SUCCESS){
            msgText.setText("isGooglePlayServicesAvailable SUCCESS");

        }else{
            GoogleApiAvailability.getErrorDialog(this, resultCode, 1).show();
        }
    }

}

Upvotes: 4

Views: 3944

Answers (2)

RubioRic
RubioRic

Reputation: 2468

Nurlan has already pointed the right answer.

Replace your method

protected void IsPlayServicesAvailable() {
    int resultCode = GoogleApiAvailability.isGooglePlayServicesAvailable(this);

    if (resultCode == ConnectionResult.SUCCESS){
        msgText.setText("isGooglePlayServicesAvailable SUCCESS");
    } else {
        GoogleApiAvailability.getErrorDialog(this, resultCode, 1).show();
    }
}

by

protected void IsPlayServicesAvailable() {
    int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);

    if (resultCode == ConnectionResult.SUCCESS){
        msgText.setText("isGooglePlayServicesAvailable SUCCESS");
    } else {
        GoogleApiAvailability.getInstance().getErrorDialog(this, resultCode, 1).show();
    }
}

Upvotes: 10

Nurlan
Nurlan

Reputation: 803

by googling, this link says: static isGooglePlayServicesAvailable(Context ctx) is deprecated, use non- static isGooglePlayServicesAvailable(Contex ctx). (And there is public static GoogleApiAvailability getInstance () for you ;) )

Upvotes: 1

Related Questions