Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法将正确的上下文传递给isGooglePlayServicesAvailable_Android_Google Maps Android Api 2 - Fatal编程技术网

Android 无法将正确的上下文传递给isGooglePlayServicesAvailable

Android 无法将正确的上下文传递给isGooglePlayServicesAvailable,android,google-maps-android-api-2,Android,Google Maps Android Api 2,我的代码中有这个函数 private boolean isGoogleServiceAvailable() { try { int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); //ERROR if (isAvailable == ConnectionResult.SUCCESS) { return true; }

我的代码中有这个函数

private boolean isGoogleServiceAvailable() {
    try {
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); //ERROR
        if (isAvailable == ConnectionResult.SUCCESS) {
            return true;
        }
        else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
            dialog.show();
        }
    }
    catch (Exception ex) {
        Toast.makeText(this, R.string.googleMapsServiceNotAvailable, Toast.LENGTH_LONG).show();
    }
    return false;
}
但是,我在这一行代码中遇到了java.lang.NullPointerException异常:

int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
可能是因为我没有正确地传递上下文

我的类声明如下所示:

public class MapEngine extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, OnItemSelectedListener {
我正在从我的主活动的onCreate调用该方法:

public class AppMain extends ListActivity{    
protected void onCreate(final Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_view);

            // InitiateMap
            mapObj.isGoogleServiceAvailable();

        }

您能告诉我如何将正确的上下文传递给isGooglePlayServicesAvailable函数吗?

我认为这是因为您使用的是未创建的活动方法。在启动地图之前,尝试从主应用程序检查播放服务

public class AppMain extends ListActivity{    
protected void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_view);

try {
            int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); //ERROR
            if (isAvailable == ConnectionResult.SUCCESS) {
                return true;
            }
            else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, AppMain.this, GPS_ERRORDIALOG_REQUEST);
                dialog.show();
            }
        }
        catch (Exception ex) {
            Toast.makeText(this, R.string.googleMapsServiceNotAvailable, Toast.LENGTH_LONG).show();
        }           



    }

您在哪里拨打iGoogle服务?您没有提供您在哪里拨打npe电话的信息。。。记住,contex在super之后是有效的。一旦创建了活动,很抱歉,请检查我更新的问题。你试过了吗,getApplicationContext?@El_Mochiq即使使用“getApplicationContext”,也会出现同样的问题`