SupportMapFragment - 无法施展从片段MapFragment [英] SupportMapFragment - cannot cast from Fragment to MapFragment

查看:150
本文介绍了SupportMapFragment - 无法施展从片段MapFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图建立一个小MapFragment一个活动,我建立,但我有一些麻烦了一切工作。我知道地图API和播放服务都安装正确,因为我做了测试教程,一切运行良好。

继文档这里,我遇到了以下问题:在我的setUpMapIfNeeded方法,我既可以使用 getFragmentManager() getSupportFragmentManager()。当我使用 getFragmentManager(),Eclipse是凉爽,但是当我跑,我收到了的NoSuchMethodError 说,该方法是不确定的。当我选择 getSupportFragmentManager(),Eclipse不喜欢它,并给了我不能投从片段MapFragment的错误。所以,这是怎么回事?有什么建议?

 私人无效setUpMapIfNeeded(){
    //做一个空检查确认,我们还没有实例化的地图。
    如果(MMAP == NULL){
        MMAP =((MapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。)的GetMap()。
        如果(MMAP!= NULL){
            //做事地图
            mMap.addMarker(新MarkerOptions()位置(位置).title伪(EXTRA_URL));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(位置,15));
            mMap.getUiSettings()setZoomControlsEnabled(假)。
        }
    }
}
 

让我知道如果有任何其他的code,我可以提供,我会很乐意很快发布。

解决方案
  

我可以用getFragmentManager()或getSupportFragmentManager()。

应该有在这里没有争论。如果 getSupportFragmentManager()是提供给你,那么你正在使用的片段支持Android包的反向移植,这是你必须使用的方法。

  

当我选择getSupportFragmentManager(),Eclipse不喜欢它,并给了我不能投从片段MapFragment的错误。

那是因为你不应该使用 MapFragment 。您正在使用片段的支持Android包的反向移植,因此,你必须使用 SupportMapFragment

Trying to put together a little MapFragment in an activity I'm building, but am having some trouble getting it all to work. I know that the Maps api and Play services are both installed correctly, as I did the test tutorial and everything worked fine.

Following the documentation here, I'm running into the following problem: In my setUpMapIfNeeded method, I can either use getFragmentManager() or getSupportFragmentManager(). When I use the getFragmentManager(), Eclipse is cool with it but when I run, I get a NoSuchMethodError saying that the method is undefined. When I opt for getSupportFragmentManager(), Eclipse doesn't like it and gives me the error "Cannot cast from Fragment to MapFragment". So what's the deal? Any suggestions?

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);
        }
    }
}

Let me know if there's any other code I can provide and I'll be happy to post it quickly.

解决方案

I can either use getFragmentManager() or getSupportFragmentManager().

There should be no debate here. If getSupportFragmentManager() is available to you, then you are using the Android Support package's backport of fragments, and this is the method that you must use.

When I opt for getSupportFragmentManager(), Eclipse doesn't like it and gives me the error "Cannot cast from Fragment to MapFragment".

That is because you should not be using MapFragment. You are using the Android Support package's backport of fragments, and therefore you must use SupportMapFragment.

这篇关于SupportMapFragment - 无法施展从片段MapFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆