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

I ran into a bug in my application when I run 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 only shows up in Android Lollipop, the same app works without issue on 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>

      

A snippet of Google Map v2 is loaded into 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 am using AndroidStudio, so here 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 understand what I did wrong. Is it possible the problem is caused by Lollipop itself?

Thanks in advance,

Martin


* Checking devices:

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


source to share


3 answers


Try moving getMap

to onResume

. I honestly don't know the exact answer, its just trial and error and voila! his job.

But what I think translates it to onResume

, make sure Activity/Fragment

( onCreate

) was created and run ( onStart

).



UPDATE

And update google play services

to the latest version.

+1


source


I have exactly the same problem on HTC M8 5.0, but only moving the code on onResume

didn't help. Instead, I found that it worked if I used getChildFragment()

instead of the usual one getFragmentManager()

.

The code I am now using 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();

      

Hope this helps!

+6


source


I have the same problem. FragmentManager issue. Best solution for me:

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();
    }

      

+2


source







All Articles