-2

I want to implement a map on a fragment. But when I call getMapAsync() it gives a NullpointerException:

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
                                                                            at kontrolleur.radar.de.radar.fragment.MapFragment.inOnCreateView(MapFragment.java:33)

MapFragment.class:

public class MapFragment extends BaseFragment implements OnMapReadyCallback {

public static MapFragment create(){
    return new MapFragment();
}

@Override
public int getLayoutResId() {
    return R.layout.fragment_map;
}

@Override
public void inOnCreateView(View root, @Nullable ViewGroup container, Bundle savedInstanceState) {
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_container);
    mapFragment.getMapAsync(this);
}

private GoogleMap googleMap;

@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;
}
}

The fragment lays within a ViewPager btw.

Here's the fragment_map.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.gms.maps.MapView
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

My Manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kontrolleur.radar.de.radar">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity android:name=".MainActivity"
        android:theme="@style/FullScreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

The api key lays within the google_maps_api.xml and is valid.

What I tried:

First I replaced the mapFragment with a new Instance of SupportMapFragment. But this didnt work and put a blank screen on the fragment.

Then I tried to put the mapFragment.getMapAsync(this); into different methods (like onCreate, onViewCreated etc.). Still a nullpointerexception.

I also replaced getChildFragmentManager() with getActivity().getFragmentManager().

Am I missing something?

My MainActivity extends AppCompatActivity and BaseFragment extends Fragment if that is a help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

The problem is that your Fragment's layout XML is defining a MapView, not a SupportMapFragment. As such, the call to findFragmentById() is returning null.

Replace this tag:

<com.google.android.gms.maps.MapView
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

with this:

<fragment
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    />
Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Now I get a android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class fragment – Thomas Beyer Oct 02 '17 at 21:18
  • Just a guess, but I'm betting that it's because you're using a `` tag inside the layout for a `Fragment`, and child fragments have to be added using the `FragmentManager` returned by `getChildFragmentManager()` (and can't be added directly via XML). – Ben P. Oct 02 '17 at 21:48