Android GoogleMap or SupportMapFragment - null pointer exception

前端 未结 8 2012
心在旅途
心在旅途 2021-02-10 10:51

In my app i showing google map version2 in a fragment. but i get Null pointer exception at

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id

相关标签:
8条回答
  • 2021-02-10 10:58

    May be above answers work but what work for me is this.

     {
        SupportPlaceAutocompleteFragment supportPlaceAutocompleteFragmentSearch;
        FragmentManager fragmentManagerSearch = getChildFragmentManager();
        supportPlaceAutocompleteFragmentSearch = 
                                                (SupportPlaceAutocompleteFragment)
                                                fragmentManagerSearch.findFragmentById(R.id.place_autocomplete_fragment_search);
        }
    
    0 讨论(0)
  • 2021-02-10 11:00

    Try changing to

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

    You're using getFragmentManager(), but casting it to SupportMapFragment

    You also need to extend FragmentActivity: http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html

    0 讨论(0)
  • 2021-02-10 11:03

    if you developed your code with Kotlin, and want to add MapFragment to another Fragment you need to use SupportMapFragment but if you do it, you will get null exception so you should use childFragmentManager rather than supportFragmentManager and the code should be in onCreateView not in onCreate just add below code:

    private lateinit var mapFragment : SupportMapFragment
    
     mapFragment = childFragmentManager.findFragmentById(R.id.mapview) as SupportMapFragment
        mapFragment.getMapAsync { googleMap ->
            this.googleMap = googleMap
            setGoogleMapSettings()
        }
    
    0 讨论(0)
  • 2021-02-10 11:04

    Try to add tools:context=".YourActivity" to the fragment xml:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".YourActivity" />
    
    0 讨论(0)
  • 2021-02-10 11:07

    None of these solutions fixed it for me -- but this answer from Mark Murphy (https://code.google.com/p/android/issues/detail?id=77714) did:

    Use getChildFragmentManager() to access a nested fragment, not getFragmentManager(). http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager%28%29

    i.e.:

    public class MapFragment extends Fragment {
        private GoogleMap map;
        private SupportMapFragment mapFragment;
        ...
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
            View view = inflater.inflate(R.layout.map_view, vg, false);
            FragmentManager fm = getChildFragmentManager();
            mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                public void onMapReady(GoogleMap googleMap) {
                    map = googleMap;
                    initMap();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-02-10 11:07

    After hours of search, I did in this way:

     public class MapFragment extends Fragment {
        private GoogleMap map;
        SupportMapFragment mapFrag;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            setHasOptionsMenu(true);
            View rootView = inflater.inflate(R.layout.fragment_map, container,
                    false);
    
            fm = getActivity().getSupportFragmentManager();
            map = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap();
    
            return rootView;
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题