title: Select photo from Android Gallery then display it in a fragment?
body: <p>I am developing an Android app using fragments and would like the user to be able to pick an image for scanning purposes. I have a fragment that allows the user to select their desired source (camera, hand-drawn, or gallery). When the user selects Gallery, then another fragment should display the image. I already know how I am supposed to get the image into an <code>ImageView</code> but keep getting this error with the Fragments:</p>
  
  <pre><code>12-10 17:30:26.405: E/AndroidRuntime(6560): FATAL EXCEPTION: main
  12-10 17:30:26.405: E/AndroidRuntime(6560): java.lang.RuntimeException: Unable to resume activity {org.nathanmoos.artificialIntelligence.clockreader/org.nathanmoos.artificialIntelligence.clockreader.ChooseSourceActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2241)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.os.Handler.dispatchMessage(Handler.java:99)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.os.Looper.loop(Looper.java:130)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.ActivityThread.main(ActivityThread.java:3835)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at java.lang.reflect.Method.invokeNative(Native Method)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at java.lang.reflect.Method.invoke(Method.java:507)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at dalvik.system.NativeStart.main(Native Method)
  12-10 17:30:26.405: E/AndroidRuntime(6560): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.view.ViewGroup.addView(ViewGroup.java:1871)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.view.ViewGroup.addView(ViewGroup.java:1828)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.view.ViewGroup.addView(ViewGroup.java:1808)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:848)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1371)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:499)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.Activity.performStart(Activity.java:3791)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.Activity.performRestart(Activity.java:3821)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.Activity.performResume(Activity.java:3826)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231)
  12-10 17:30:26.405: E/AndroidRuntime(6560):     ... 10 more
  </code></pre>
  
  <p>Basically it looks like there's an issue with my <code>FragmentTransaction</code> usage. Here's my code:</p>
  
  <pre><code>frag = new GalleryFragment();
  frag.setImagePath(imagePath);
  
  FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
  tx.replace(R.id.fragment_space, frag);
  tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  </code></pre>
  
  <p>and my layout file:</p>
  
  <pre><code>   &lt;FrameLayout 
         android:id="@+id/fragment_space"
         android:layout_width="0dp"
         android:layout_height="match_parent"
         android:layout_weight="65"/&gt;
  </code></pre>