SupportMapFragment - 无法从Fragment转换为MapFragment

use*_*853 17 android google-maps-android-api-2

试图在我正在构建的一个活动中组合一个MapFragment,但是在将它全部用于工作方面遇到了一些麻烦.我知道Maps api和Play服务都安装正确,因为我做了测试教程,一切正常.

按照这里的文档,我遇到了以下问题:在我的setUpMapIfNeeded方法中,我可以使用getFragmentManager()getSupportFragmentManager().当我使用getFragmentManager()它时,Eclipse很酷,但是当我运行时,我得到一个NoSuchMethodError说法,该方法是未定义的.当我选择时getSupportFragmentManager(),Eclipse不喜欢它并且给我错误"无法从Fragment转换为MapFragment".那是什么交易?有什么建议?

private void setUpMapIfNeeded() {
    //Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (mMap != null) {
            //do things to the map
            mMap.addMarker(new MarkerOptions().position(LOCATION).title(EXTRA_URL));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LOCATION,15));
            mMap.getUiSettings().setZoomControlsEnabled(false);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我能提供任何其他代码,请告诉我,我很乐意快速发布.

Com*_*are 82

我可以使用getFragmentManager()或getSupportFragmentManager().

这里不应该有争论.如果getSupportFragmentManager()可以使用,那么您正在使用Android Support包的片段backport,这是您必须使用的方法.

当我选择getSupportFragmentManager()时,Eclipse不喜欢它并且给我错误"无法从Fragment转换为MapFragment".

那是因为你不应该使用MapFragment.您正在使用Android Support软件包的片段backport,因此您必须使用SupportMapFragment.

  • 有时愚蠢的错误会帮助其他人(可能是新手)掌握一个概念,所以永远不会觉得太傻了;) (2认同)