Q:

Android - MapView within a fragment

I followed many tutorials and read lots of questions here, and maybe I got in some confusion.
I'm developing an app which presents a MainActivity with an action bar, splitting the activity in 3 tabs (each tab is a fragment). On the first tab I want to show a Google Maps (with a MapView, which I guess it's easier than MapFragment), but I'm facing a NullPointerException on instantiating the map. Here's the logcat:
05-25 06:14:26.740: E/AndroidRuntime(918): FATAL EXCEPTION: main
05-25 06:14:26.740: E/AndroidRuntime(918): Process: com.example.mypackage, PID: 918
05-25 06:14:26.740: E/AndroidRuntime(918): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypackage/com.example.mypackage.activities.MainActivity}: java.lang.NullPointerException
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.os.Looper.loop(Looper.java:136)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-25 06:14:26.740: E/AndroidRuntime(918):  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 06:14:26.740: E/AndroidRuntime(918):  at java.lang.reflect.Method.invoke(Method.java:515)
05-25 06:14:26.740: E/AndroidRuntime(918):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-25 06:14:26.740: E/AndroidRuntime(918):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-25 06:14:26.740: E/AndroidRuntime(918):  at dalvik.system.NativeStart.main(Native Method)
05-25 06:14:26.740: E/AndroidRuntime(918): Caused by: java.lang.NullPointerException
05-25 06:14:26.740: E/AndroidRuntime(918):  at com.example.mypackage.fragments.MapFragment.onCreateView(MapFragment.java:30)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.Activity.performStart(Activity.java:5241)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
05-25 06:14:26.740: E/AndroidRuntime(918):  ... 11 more

And here's the MapFragment
public class MapFragment extends Fragment {

    MapView mapView;
    GoogleMap map;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_map, container, false);
        // Gets the MapView from the XML layout and creates it
        mapView = (MapView) v.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        // Gets to GoogleMap from the MapView and does initialization stuff
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);

        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls

        MapsInitializer.initialize(this.getActivity());

        // Updates the location and zoom of the MapView
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(0, 0), 10);
        map.animateCamera(cameraUpdate);
        return v;
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
}

So the call that generates the error (line 30) is           
map.getUiSettings().setMyLocationButtonEnabled(false);
Finally, this is fragment_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

If it is any help here's my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mypackage"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <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" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my_api_key" />

        <activity
            android:name="com.example.mypackage.activities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Anyone has idea why? Thanks.

A:

In the android documentation, it says about MapView.getMap():
Null if the view of the map is not yet ready. This can happen when Google Play services is not available. If Google Play services becomes available afterwards, calling this method again will initialize and return the GoogleMap.
Do you have the google play services app installed on the device you launched your app?

