0

INTRODUCTION

For my app, I need to manage the orientation due to I'm working with fragments. In portrait mode, I start GoogleMapsFragment in a new Activity. But in landscape mode, I start GoogleMapsFragment at the right side of main Activity.

Have to say that in protrait mode everything wortks fine. I call GoogleMapsFragment and Maps starts on a new Activity.

PROBLEM

But in landscape mode, when I start GoogleMapsFragment, throws me a NullPointerException and gets out of the app.

This is a snippet of code of GoogleMapsFragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View mainView = inflater.inflate(R.layout.google_maps, container, false);

    mMapView = (MapView) mainView.findViewById(R.id.map_view);
    mMapView.onCreate(savedInstanceState);

    mMap = mMapView.getMap();
    mMap.getUiSettings().setMyLocationButtonEnabled(false);
    mMap.setMyLocationEnabled(true);

    //...

Where I'm getting the null is when I do mMap = mMapView.getMap(), it seems that it isn't returning any value to mMap, so in the next step, as mMap is null, it throws the exception.

This is the layout "google_maps.xml":

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.gms.maps.MapView 
        android:id="@+id/map_view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" />  
</LinearLayout>

I don't know why is working in portrait mode but no in landscape mode. Also have to say that the portrait mode has been tested on a real device, and landscape mode in the emulator, maybe this could be the cause...

If this is the reason, how could I test it?

UPDATE --

Using Google API 4.4.2 on the emulator, this continues the NullPointerException issue but this is what the LogCat also shows:

Google Play services out of date.  Requires 4132500 but found 4033530
masmic
  • 3,526
  • 11
  • 52
  • 105

1 Answers1

0

I think you are calling it too early (check here: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment#getMap%28%29, onCreateView is not done yet) You also might want to still check if it is null.

Emulator could be the reason too, if the services are not included there. (How to download Google Play Services in an Android emulator?)

Community
  • 1
  • 1
Maximosaic
  • 604
  • 6
  • 14
  • Could be something of that, but... the thing about calling it too early... if it works in portrait shouldn't work too on landscape? Also, about GPServices, it says that above android 4.2.2 the emulator can work with these services, and I'm working with android 4.4.2 – masmic Feb 13 '14 at 10:37