android – MapFragment返回null

mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doesn't already exist.
        if (mMapFragment == null) {
          mMapFragment = SupportMapFragment.newInstance();


            // Then we add it using a FragmentTransaction.
            FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(MapLay.getId(),mMapFragment,MAP_FRAGMENT_TAG);
             fragmentTransaction.commit();


            mMap=mMapFragment.getMap();

通过此代码映射可见但无法访问地图

MMAP = mMapFragment.getMap();显示空值错误如何解决此问题

解决方法

更新1:不推荐使用getMap()

最好使用MapFragment / SupportMapFragment的getMapAsync()方法.示例如何使用下面显示方法(从他们的documentation复制).

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class MapPane extends Activity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        LatLng sydney = new LatLng(-33.867,151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    }

}

引用Google的MapFragment/SupportMapFragment

A GoogleMap can only be acquired using getMap() when the underlying
maps system is loaded and the underlying view in the fragment exists.
This class automatically initializes the maps system and the view;
however you cannot be guaranteed when it will be ready because this
depends on the availability of the Google Play services APK. If a
GoogleMap is not available,getMap() will return null.

在您的代码中,您可以立即检索GoogleMap AFTER Committing MapFragment.等到MapFragment完全加载到活动上,这样您就可以获得GoogleMap.

也许,您可以使用界面将MapMap从MapFragment传递到Activity,就像这样.

public class MyMapFragment extends SupportMapFragment
{
  private MapCallback callback;

  public void setMapCallback(MapCallback callback)
  {
    this.callback = callback;
  }

  public static interface MapCallback
  {
     public void onMapReady(GoogleMap map);
  }

  @Override public void onActivityCreated(Bundle savedInstanceState)
  {
     super.onActivityCreated(savedInstanceState);
     if(callback != null) callback.onMapReady(getMap());     
  }
}


public class MyActivity extends Activity implements MyMapFragment.MapCallback
{
   // .........
  @Override
  public void onCreate(Bundle onsavedInstanceState)
  {
        mMapFragment = (MyMapFragment) getSupportFragmentManager()
                .findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doesn't already exist.
        if (mMapFragment == null) {
               mMapFragment = MyMapFragment.newInstance();

               mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded

               // Then we add it using a FragmentTransaction.
               FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
               fragmentTransaction.add(MapLay.getId(),MAP_FRAGMENT_TAG);
               fragmentTransaction.commit();

         }
         else
         {
               mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded
         }

  @Override
  public void onMapReady(GoogleMap map)
  {
     // Do what you want to map
  }

}

相关文章

以下为个人理解,如错请评 CE: 凭据加密 (CE) 存储空间, 实际路径/data/user_ce/ DE: 设备加密 (DE) 存...
转载来源:https://blog.csdn.net/yfbdxz/article/details/114702144 用EventLog.writeEvent打的日志(或...
事件分发机制详解 一、基础知识介绍 1、经常用的事件有:MotionEvent.ACTION_DOWN,MotionEvent.ACTION...
又是好久没有写博客了,一直都比较忙,最近终于有时间沉淀和整理一下最近学到和解决的一些问题。 最近进...
Android性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向...
android的开源库是用来在android上显示gif图片的。我在网上查了一下,大家说这个框架写的不错,加载大的...