mapFragment.getMapAsync(this) - NullPointerException

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I am using com.google.android.gms:play-services-maps:7.5.0 version of Google Maps services. When trying to call the below I get java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference.

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,                          Bundle savedInstanceState) {     SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()             .findFragmentById(R.id.map);     mapFragment.getMapAsync(this); //error      return inflater.inflate(R.layout.fragment_example_map, container, false); }

fragment_example_map.xml file:

       

回答1:

You are attempting to find a fragment before it exists. You indicate that the layout that has the fragment is fragment_example_map.xml. However, you are trying to find the map fragment before you inflate that layout file. This will not work.

Beyond that, you appear to be trying to get at the map fragment from inside another fragment, in that fragment's onCreateView() method. I do not know why you are nesting fragments here, as it seems like it will make your code more complex and more fragile for no obvious benefit.



回答2:

My comment in maps with fragment , first you should refrenece your view as you will call something from it , this is why I inflate my view first
View view = inflater.inflate(R.layout.activity_maps, null, false);

Second call child fragment manager this.getChildFragmentManager() instead of getActivity().getSupportFragmentManager()

Here is the full example to view map

import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;  import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions;  public class ClinicFragment extends Fragment implements OnMapReadyCallback {      private GoogleMap mMap;      public static ClinicFragment newInstance() {         ClinicFragment fragment = new ClinicFragment();         return fragment;     }      @Nullable     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         View view = inflater.inflate(R.layout.activity_maps, null, false);          SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);          return view;     }       /**      * Manipulates the map once available.      * This callback is triggered when the map is ready to be used.      * This is where we can add markers or lines, add listeners or move the camera. In this case,      * we just add a marker near Sydney, Australia.      * If Google Play services is not installed on the device, the user will be prompted to install      * it inside the SupportMapFragment. This method will only be triggered once the user has      * installed Google Play services and returned to the app.      */     @Override     public void onMapReady(GoogleMap googleMap) {         mMap = googleMap;          // Add a marker in Sydney and move the camera         LatLng sydney = new LatLng(-34
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!