3

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
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
  • 1
    Try to move the `getMap` code to the `onResume` – Blaze Tama Dec 05 '14 at 13:08
  • Works! Thank you so much, saved my weekend ;) But why is that? Is instantiation of the Maps Fragment by ART not as fast as it goes by good old Dalvik? If you want I will accept your answer. – Martin Pfeffer Dec 05 '14 at 13:20
  • Glad to help :) I tried my best to explain what i dont really know in my answer LOL – Blaze Tama Dec 05 '14 at 13:29
  • Edit: sadly it doesn't work. I just oversaw a try catch... Still the same error on getMap()... I will read this: http://stackoverflow.com/questions/13722192/google-maps-android-api-v2-throws-googleplayservicesnotavailableexception-out-o – Martin Pfeffer Dec 05 '14 at 13:30
  • 1
    Yes, try to update your `google play services`. I use the latest one with `onResume` and the error never happened again – Blaze Tama Dec 05 '14 at 13:32
  • Do you use the SupportFragmentManager or the "normal" FragmentManager? – Martin Pfeffer Dec 05 '14 at 14:18

3 Answers3

6

I've got the exact same problem under HTC M8 5.0 but only moving the code to onResume did not do the trick. Instead I found that it worked if I used getChildFragment() instead of the normal getFragmentManager().

The code I now use to get the reference to the map object (under onResume):

MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapsfragment_map);

if(mapFragment == null){
    return;
}

map = mapFragment.getMap();

I hope this maybe helps!

Jacob Hagstedt
  • 103
  • 2
  • 6
2

I've got the same problem. Problem with FragmentManager. The best solution for me is:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        sMap = ((MapFragment) (getChildFragmentManager().findFragmentById(R.id.location_map)).getMap();
    } else {
        sMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.location_map)).getMap();
    }
Vitalii Obideiko
  • 1,726
  • 1
  • 13
  • 16
1

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.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • Should'nt you put that code into `onViewCreated()` if it's a `Fragment`? – shkschneider Dec 05 '14 at 15:02
  • @shkschneider Thanks for reminding. I updated my answer. I think the code is safer in `onResume`, both `fragment` and `activity` has it on its lifecycle – Blaze Tama Dec 06 '14 at 00:54