SupportMapFragment.getmap() returns null

拜拜、爱过 提交于 2019-11-26 12:47:11

问题


I\'m trying to load SupportMapFragment dynamically in a fragment, here is my onCreateView() method:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View contentView = inflater.inflate(R.layout.frag_map_layout, null);
            shopDtos=ShopListFragment.shopDtos;
            fragment =SupportMapFragment.newInstance();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.map_content, fragment);
            ft.commit();
            map = fragment.getMap();
            mapMarker = map.addMarker(new MarkerOptions().position(new LatLng(0, 0))
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.maps_pin)));
        return contentView;
    }

Unfortunately, I get the GoogleMap map as null. Any suggestions on to how create a mapfragment dynamically?


回答1:


map takes some time to load, so you need to run your code in handler -->

Handler handler = new Handler();
handler.postDelayed(new Runnable() 

   @Override
   public void run() {
       GoogleMap googleMap = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true)).getMap();
       FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.map_content, fragment);
        ft.commit();
       if(googleMap != null) {
          googleMap.addMarker(new MarkerOptions().position(result)).setVisible(true);

          // Move the camera instantly to location with a zoom of 15.
          googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(result, 15));

          // Zoom in, animating the camera.
          googleMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);

          googleMap.getUiSettings().setZoomControlsEnabled(false);
          googleMap.getUiSettings().setCompassEnabled(false);
          googleMap.getUiSettings().setMyLocationButtonEnabled(false);

          handler.removeCallbacksAndMessages(null);
       }

       else {
            handler.postDelayed(this, 500);
       }
 }, 500);



回答2:


Just try using the following code. It worked for me.

public class MapFragment extends SupportMapFragment {

    GoogleMap mapView;
    private Context context;

    @Override
    public void onCreate(Bundle arg0) {
        super.onCreate(arg0);
    }

    @Override
    public View onCreateView(LayoutInflater mInflater, ViewGroup arg1,
        Bundle arg2) {
        View view = super.onCreateView(mInflater, arg1, arg2);

        return view;
    }

    @Override
    public void onInflate(Activity arg0, AttributeSet arg1, Bundle arg2) {
        super.onInflate(arg0, arg1, arg2);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        mapView = getMap();
    }
}



回答3:


There's a bunch of duplicate questions that go around the same topic... I can't seem to figure out how to flag it as a duplicate and then put one answer on it...

Here's the link to the other one: Android SupportMapFragment.getMap() returns null

And the potential answer posted there as well:

Check your layout xml file. I noticed that I wasn't referencing: android:name="com.google.android.gms.maps.SupportMapFragment" but rather: android:name="com.google.android.gms.maps.MapFragment"

The name should be: android:name="com.google.android.gms.maps.SupportMapFragment"




回答4:


It happened with me once in a new purchased device with Google play services not yet installed /updated. With other notes mentioned of course also consider this one.




回答5:


An up to date solution for this problem is to use getMapAsync (OnMapReadyCallback callback) instead.




回答6:


getChildFragmentManager().findFragmentById(R.id.map)

Try getChildFragmentManager() in Fragment.



来源:https://stackoverflow.com/questions/19219255/supportmapfragment-getmap-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!