0

我正在使用 Android Studio 1.1 和 AP1 21(课程需要的版本)。我使用Google Maps Activity.

在自动生成的代码中,我收到以下错误消息:Error:(48, 21) error: cannot find symbol method getMap(),在setUpMapIfNeeded方法中:

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    } 

任何想法如何解决这个问题?谢谢!

4

1 回答 1

0

我使用相同的方法,我得到了同样的错误。我通过实现 OnMapReadyCallback 来修复它。

首先实现 OnMapReadyCallback:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
    ......
    ....

新的 setUpMapIfNeeded() 方法:

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mf.getMapAsync(this);
    }
}

并在覆盖的 onMapReady 中调用 setUpMap():

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

setUpMap() 方法或其他方法没有变化。我希望它有所帮助。

于 2016-11-27T10:02:28.160 回答