非静态方法是GooglePlayServicesAvailable,并且无法从静态上下文引用getErrorDialog

时间:2016-03-25 10:44:34

标签: java android android-studio google-cloud-messaging google-play-services

我正在编写此代码并且它显示错误非静态方法GoogleApiAvailability.isGooglePlayServicesAvailable(上下文上下文)和GoogleApiAvailability.getErrorDialog(Activity activity,int errorCode,int requestCode)无法从静态上下文引用。

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

}

2 个答案:

答案 0 :(得分:8)

Nurlan已经指出了正确的答案。

替换你的方法

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

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

通过

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

答案 1 :(得分:1)

通过Google搜索,这个link说:静态 isGooglePlayServicesAvailable(Context ctx)已被弃用,请使用非静态 isGooglePlayServicesAvailable(Contex ctx)。 (并且public static GoogleApiAvailability getInstance ()为你;))