【问题标题】:Error:(48, 21) error: cannot find symbol method getMap()错误:(48, 21) 错误: 找不到符号方法 getMap()
【发布时间】:2016-08-22 18:33:04
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    我使用了相同的方法,我得到了同样的错误。我通过实现 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() 方法或其他方法没有变化。希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多