尊敬的社区,我正在学习ANDROID中的Google Maps集成,并且已经了解了很多东西,拥有权限,并使用了最新的GoogleApiAvailability,但仍然遇到2个我无法解决的错误。我在Cannot Resolve Method isUserRecoverableError(int)方法中得到non-static method getErrorDialog cannot be referenced from a static context in the end of codepublic boolean ServicesOK()
以下是Java代码:(如果需要其他任何信息,请告诉我,以便可以使用Android Google Maps概念正确地进行引导:))

package com.example.testmap;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private static final int ERROR_DIALOG_REQUEST = 9901;

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

        if(ServicesOK()) {
            Toast.makeText(this, "Can't connect to mapping services", Toast.LENGTH_SHORT).show();}

            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        GoogleMap mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    public boolean ServicesOK(){
        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int isAvailable = googleApiAvailability.isGooglePlayServicesAvailable(this);

        if(isAvailable== ConnectionResult.SUCCESS){
            return  true;
        }
        else if(GoogleApiAvailability.isUserRecoverableError(isAvailable)){
            Dialog dialog;
            dialog= GoogleApiAvailability.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
            dialog.show();
        }
        else{
            Toast.makeText(this, "Can't connect to mapping services", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}

最佳答案

isUserRecoverableError方法来自GooglePlayServicesUtil,而不是GoogleApiAvailability。但是,正如您在文档中看到的那样,此方法已被弃用,您现在应该使用GoogleApiAvailability.getInstance().isUserResolvableError()

对于第二个错误,您正在尝试访问getErrorDialog,但这不是静态方法。您应该改为GoogleApiAvailability.getInstance().getErrorDialog

10-07 18:30