1

我在 FragmentActitvity 中显示了谷歌地图,它实现并打开这个活动我从另一个活动(比如说活动 A)开始一个意图,点击一个按钮。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MapActivity.class);
                startActivity(intent);

        }
    });

我的 MapActivity 是这样的:

public class MapActivity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {
    private GoogleMap googleMap;
    private LocationClient locationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.setMyLocationEnabled(true);

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }

    locationClient = new LocationClient(this, this, this);
    locationClient.connect();
    mLocationRequest = LocationRequest.create();

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(5 * 1000);
    mLocationRequest.setSmallestDisplacement(0.1f);
    mLocationRequest.setFastestInterval(5 * 1000);
}
}

MapActivity 还有其他接口的实现方法。但是当我点击按钮打开 MapActivity 时,我得到了错误:

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.google.android.location.internal.GoogleLocationManagerService.START }

我在谷歌上搜索,发现我需要创建一个明确的意图。但我不确定在我的情况下如何做到这一点。

4

1 回答 1

0

尝试使用 Android SDK Manager 升级您的 Android SDK 构建工具。选择构建工具的最新版本。然后在你的 build.gradle

compileSdkVersion 22
buildToolsVersion "22.0.1"

最好的方法是在新的 GoogleApiClient 上升级,你的新 MapActivity 应该是这样的

public class MapActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
    private GoogleMap googleMap;

    private GoogleApiClient mGoogleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        if (googleMap == null) {
            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            googleMap.setMyLocationEnabled(true);

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }

    @Override
    public void onConnected(Bundle bundle) {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(5 * 1000);
        locationRequest.setSmallestDisplacement(0.1f);
        locationRequest.setFastestInterval(5 * 1000);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {
        // handle new location
    }
}

你应该使用最后的播放服务。在你的 build.gradle

dependencies {

    compile 'com.google.android.gms:play-services-location:7.5.0'
    compile 'com.google.android.gms:play-services-maps:7.5.0'
}
于 2015-08-05T12:12:19.070 回答