title: MapView inside Fragment - specified child already has a parent
body: <p>I'm trying to show a  MapView inside a fragment (using the hacked compatibility library). The following has worked just fine in the past:</p>
  
  <ul>
  <li>fragment's <code>onCreateView()</code> simply returns a new <code>FrameLayout</code></li>
  <li>fragment's <code>onActivityCreated()</code> gets the MapView from the Acitivity and adds it to its view hierarchy</li>
  <li><code>onDestroyView()</code> removes the MapView from its view hierarchy</li>
  </ul>
  
  <p>Now I would like the fragment to use a layout defined in xml so that I can have some other UI stuff. Putting the <code>MapView</code> element in the layout file always crashes, so I'm doing it this way:</p>
  
  <p><strong>map_screen_fragment.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" &gt;
  
  
      &lt;FrameLayout
          android:id="@+id/map_container"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" &gt;
      &lt;/FrameLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>My <code>MapScreenActivity</code> holds the actual <code>MapView</code>, and the fragment calls <code>getMapView()</code>, so I don't run into the "can't have more than one MapView" issue:</p>
  
  <p><strong>MapScreenActivity.java</strong></p>
  
  <pre><code>public class MapScreenActivity extends FragmentActivity {
      protected Fragment fragment;
      protected MapView mapView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.single_pane_empty);
  
          if (savedInstanceState == null) {
              fragment = new MapScreenFragment();
  
              getSupportFragmentManager().beginTransaction().add(R.id.root_container, fragment)
                      .commit();
          }
      }
  
      public MapView getMapView() {
          if (mapView == null) {
              mapView = new MapView(this, getResources().getString(R.string.maps_api_key));
          }
  
          return mapView;
      }
  }
  </code></pre>
  
  <p><strong>MapScreenFragment.java</strong></p>
  
  <pre><code>public class MapScreenFragment extends Fragment {
      protected ViewGroup mapContainer;
      protected MapView mapView;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
          View root = inflater.inflate(R.layout.map_screen_fragment, container);
          mapContainer = (ViewGroup) root.findViewById(R.id.map_container);
          return root;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          mapView = ((MapScreenActivity) getActivity()).getMapView();
          mapView.setClickable(true);
          mapView.setBuiltInZoomControls(true);
  
          mapContainer.addView(mapView);
  
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          mapContainer.removeView(mapView);
      }   
  
  }
  </code></pre>
  
  <p>In theory, this should all work the same way as the <code>new FrameLayout</code> method first described. However, I get this every time:</p>
  
  <pre><code>02-24 18:01:28.139: E/AndroidRuntime(502): FATAL EXCEPTION: main
  02-24 18:01:28.139: E/AndroidRuntime(502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapfragment/com.example.mapfragment.MapScreenActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.os.Handler.dispatchMessage(Handler.java:99)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.os.Looper.loop(Looper.java:130)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.main(ActivityThread.java:3683)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at java.lang.reflect.Method.invokeNative(Native Method)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at java.lang.reflect.Method.invoke(Method.java:507)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at dalvik.system.NativeStart.main(Native Method)
  02-24 18:01:28.139: E/AndroidRuntime(502): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addView(ViewGroup.java:1871)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addView(ViewGroup.java:1828)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.view.ViewGroup.addView(ViewGroup.java:1808)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.NoSaveStateFrameLayout.wrap(Unknown Source)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.BackStackRecord.run(Unknown Source)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.support.v4.app.FragmentActivity.onStart(Unknown Source)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.Activity.performStart(Activity.java:3791)
  02-24 18:01:28.139: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620)
  02-24 18:01:28.139: E/AndroidRuntime(502):  ... 11 more
  </code></pre>
  
  <p>I've tried removing the the <code>MapView</code> from it's parent before returning from <code>getMapView()</code>, and that still crashes. I really don't understand why this approach doesn't work, any help at all would be appreciated.</p>