title: Add a fragment containing Google Maps API V2 in Android Main Activity
body: <p>I have been trying to insert a Map in my main activity by inflating the map layout in the fragment and then trying to pass it to the activity through fragment manager. I am getting a Fatal Exception and I am not quite sure if what I am trying to do is doable. </p>
  
  <p>MainActivity:</p>
  
  <pre><code>package com.example;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.view.Menu;
  import android.view.MenuItem;
  
  public class MainActivity extends FragmentActivity {
  
      private Fragment mMapFragment;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          FragmentManager fm = getSupportFragmentManager();
          mMapFragment = fm.findFragmentById(R.id.container);
  
          if (savedInstanceState == null) {
               fm.beginTransaction()
                  .add(R.id.container, mMapFragment)
                  .commit();
          }
      }
   }
  </code></pre>
  
  <p>This is my fragment: </p>
  
  <pre><code>    package com.example;
  
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.google.android.gms.maps.SupportMapFragment;
  
  public class RunMapFragment extends SupportMapFragment {
  
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_main, container, false);
  
          return rootView;
      }
  
  }
  </code></pre>
  
  <p>Activity_main (with container as ID) is just a simple FrameLayout, while fragment_main is what you find below: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.MapFragment"/&gt;
  </code></pre>
  
  <p>There might be a few nonsense imports, simply because I have been playing with the code. Thanks.</p>
  
  <p>LOGCAT:</p>
  
  <pre><code>06-26 14:01:42.674: E/AndroidRuntime(6942): FATAL EXCEPTION: main
  06-26 14:01:42.674: E/AndroidRuntime(6942): Process: com.example, PID: 6942
  06-26 14:01:42.674: E/AndroidRuntime(6942): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: java.lang.NullPointerException
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.os.Handler.dispatchMessage(Handler.java:102)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.os.Looper.loop(Looper.java:136)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.ActivityThread.main(ActivityThread.java:5001)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at java.lang.reflect.Method.invokeNative(Native Method)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at java.lang.reflect.Method.invoke(Method.java:515)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at dalvik.system.NativeStart.main(Native Method)
  06-26 14:01:42.674: E/AndroidRuntime(6942): Caused by: java.lang.NullPointerException
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:384)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at com.example.MainActivity.onCreate(MainActivity.java:24)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.Activity.performCreate(Activity.java:5231)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  06-26 14:01:42.674: E/AndroidRuntime(6942):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
  </code></pre>
  
  <p>SOLVED: 
  I have removed the fragment from the xml, inserted it in the activity xml and passed onCreateView (from the fragment class) to the superclass. </p>