Google Maps Fragment returning null inside a fragment

前端 未结 8 838
花落未央
花落未央 2020-11-27 18:05

So I have an empty fragment that contains a map fragment. Whenever I try to activate the fragment containing the map, my app crashes and returns a null pointer error on this

相关标签:
8条回答
  • 2020-11-27 18:30

    Could this be an issue of migrating to a target sdk version of 21? If so, the following solution may be of interest to you:

    MapFragment or MapView getMap() returns null on Lollipop

    0 讨论(0)
  • 2020-11-27 18:33

    The problem may be related to the fact that your targetSdkVersion is higher than the version of Android Support Library used.

    That was my case, in one of the projects we've been working on targetSdkVersion was 21, while Support Library version was 19.0.0. Changing targetSdkVersion to 19 fixed the problem.

    0 讨论(0)
  • 2020-11-27 18:34

    Try using SupportMapFragment instead of MapFragment. This code works for me for a Fragment, just tried it:

    import android.os.Bundle;
    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.SupportMapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.Marker;
    import com.google.android.gms.maps.model.MarkerOptions;
    
    public class MapFragment extends Fragment {
    
    private GoogleMap map;
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_map, container,
                false);
    
        // Get a handle to the Map Fragment
        map = ((SupportMapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();
        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));
    
        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
    
        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    
        return rootView;
    }
    }
    

    and your layout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
    <fragment
        android:id="@+id/map"
        android:name="com.example.test.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-27 18:38

    Your class extends from Fragment, and you are using a Fragment declared in the layout, but nested fragment only work when they are added dynamically.

    So, you have two options:

    1) Extending from FragmentAcivity instead of Fragment and applying the proper methods.

    2) Add the Fragment dynamically, as follow:

    //ReturnVisitDetails

    MapFragment mapFragment = new MapFragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.rl_map_container, mapFragment).commit();
    

    //Layout

    <FrameLayout
            android:id="@+id/rl_map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    

    --UPDATED--

    A complete example to use Google Maps inside a Fragment:

    //TestFragmentActivity

    public class TestFragmentActivity extends android.support.v4.app.FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_fragment_activity);
        }
    
    }
    

    //TestFragment

    public class TestFragment extends android.support.v4.app.Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.test_fragment, container, false);
    
            CustomMapFragment mapFragment = new CustomMapFragment();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.add(R.id.map_container, mapFragment).commit();
            return rootView;
        }
    
    }
    

    //CustomMapFragment

    public class CustomMapFragment extends com.google.android.gms.maps.SupportMapFragment {
    
        private final LatLng HAMBURG = new LatLng(53.558, 9.927);
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            GoogleMap googleMap = getMap();
            googleMap.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        }
    
    }
    

    //test_fragment_activity.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/map"
            class="your_package.TestFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </RelativeLayout>
    

    //test_fragment.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-27 18:40

    This issue happened to me when I tried to port my Eclipse project to Android Studio project.

    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
    

    I used it with a Fragment Activity like this:

    ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapToday)).getMap();
    

    However, the problem was, earlier I was using a specific version of Google Play Services (4.3.23).

    But the Gradle build select the most recent version (compile 'com.google.android.gms:play-services:+')

    I tried some other specific version like (6.1.71). But still gave null point exception.

    I used the same old version as I used before and it worked.

    compile 'com.google.android.gms:play-services:4.3.23'
    

    This works for me as a temporary solution. However, this will be a problem when I need to update to a new play services library.

    It seems that there is no specific map library with this version available for Gradle build when I try like this:

    compile 'com.google.android.gms:play-services-maps:4.3.23'
    
    0 讨论(0)
  • 2020-11-27 18:43

    I have solved the issue. this line when I have a viewpager:

    mMap = ((SupportMapFragment) MainActivity.fragmentManager
                    .findFragmentById(R.id.mapView)).getMap();
    

    you have to change to this:

      mMap = ((SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.mapView)).getMap();
    
    0 讨论(0)
提交回复
热议问题