Search code examples
androidgoogle-mapsandroid-fragmentsandroid-5.0-lollipop

Map-Fragment (v2) Nullpointer (issue under Lollipop?)


I spotted a bug in my app when I launch it in Android Lollipop. Logcat says:

java.lang.NullPointerException: 

Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap.
com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

This error is only shown in Android Lollipop, the exact same app is running without issues at other devices*.


Here is the code:

<?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"
                android:orientation="vertical">

    <LinearLayout
        android:id="@+id/map_side_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/BlueGreyL3"
        android:orientation="horizontal"
        android:weightSum="100">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/map_margin_left_24dp"
            android:layout_marginStart="@dimen/map_margin_left_24dp"
            android:layout_weight="8"
            android:orientation="vertical">

            <!-- MAP -->
            <fragment
                android:id="@+id/location_map"
                class="com.google.android.gms.maps.MapFragment"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </LinearLayout>

    <!-- Ads -->
    <LinearLayout
        android:id="@+id/linlay_wrb"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    </LinearLayout>

</RelativeLayout>

The Google Map v2 Fragment is loaded in my class LocationFragments.java by calling

// View 
try {
    mView = inflater.inflate(R.layout.location_fragment, container, false);
    sMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.location_map)).getMap();
    mSideHolder = (LinearLayout) mView.findViewById(R.id.map_side_holder);

I use AndroidStudio so here is build.gradle:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"


    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
   }

[...]

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v13:21.0.0'
    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.google.android.gms:play-services:5.0.89'
}

and the Manifest:

    <!-- Google Play -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

    <!-- Maps -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value=""/>

I don't get any idea, what I've done wrong.. Is it possible that the issue is caused by Lollipop itself?

Thanks in advance,

Martin


*Test Devices:

  1. HTC One M8 - 4.4.4 (GPE / API 19) -> works
  2. Samsung S4 Mini - 4.4.2 (CM11 / API 19) -> works
  3. Samsung S2 - 4.0.3 (ICS / API 15) -> works
  4. HTC One M8 - 5.0 (LL / API 20) -> error while loading map
  5. Nexus 5 - 5.0 (LL / API 20) -> error while loading map

Solution

  • Try to move the getMap to the onResume. To be honest, i dont know the exact answer, its just trial and error and voila! its working.

    But what i think is moving it to onResume will make sure the Activity/Fragment has been created (onCreate) and started (onStart).

    UPDATE

    And update your google play services to the latest version.