【问题标题】:Android SupportMapFragment.getMap() returns nullAndroid SupportMapFragment.getMap() 返回 null
【发布时间】:2013-09-03 14:20:15
【问题描述】:

在我的以下代码中,getMap() 返回 null,这会停止应用程序。如果我不对地图做任何事情(即删除最后两行),它会正确显示。知道为什么吗?谢谢。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FragmentManager fragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment = SupportMapFragment.newInstance();
        mapFragmentId = mapFragment.getId();
        FragmentTransaction transaction =  fragmentManager.beginTransaction();
        transaction.add(R.id.my_container, mapFragment);
        transaction.commit();

        mMap = mapFragment.getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }

【问题讨论】:

标签: android google-maps-android-api-2


【解决方案1】:

重复:SupportMapFragment.getmap() returns null

检查您的布局 xml 文件。我注意到我没有引用: android:name="com.google.android.gms.maps.SupportMapFragment" 反而: android:name="com.google.android.gms.maps.MapFragment"

名称应为: android:name="com.google.android.gms.maps.SupportMapFragment"

【讨论】:

    【解决方案2】:

    如果 getMap() 返回 null,那是因为 Map 尚未准备好或 GooglePlayServices 已过时。你最好使用类似的东西:

    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if(resultCode != ConnectionResult.SUCCESS)
            {
                Builder builder = new AlertDialog.Builder(Main.this);
                builder.setMessage(getResources().getString(R.string.error_getting_maps));
                builder.setCancelable(true);
                builder.setPositiveButton("OK", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                _map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                [ETC]
            }
    

    【讨论】:

    • 谢谢,这很有帮助。当播放服务的版本过期或丢失时,您会在日志中注意到类似以下内容。 6520-6520/com.something.myapp W/GooglePlayServicesUtil:Google Play 服务已过期。需要 6171000 但找到 5089030
    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2012-05-28
    • 2014-12-05
    • 2021-07-07
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多