错误:(48,21)错误:找不到符号方法getMap()

时间:2016-08-22 18:33:04

标签: android-studio google-maps-android-api-1

我使用的是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();
            }
        }
    } 

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

1 个答案:

答案 0 :(得分: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);
    }
}

并调用setUpMap()覆盖onMapReady:

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

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