2011-12-11 65 views
0

我正在開發一個使用片段的Android應用程序,並希望用戶能夠爲掃描目的選擇一個圖像。我有一個片段,允許用戶選擇他們想要的來源(相機,手繪或圖庫)。當用戶選擇圖庫時,另一個片段應顯示圖像。我已經知道我應該如何獲取圖像到ImageView但不斷收到此錯誤與片段:從Android Gallery中選擇照片然後將其顯示在一個片段中?

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 

基本上它看起來像有一個與我FragmentTransaction使用的問題。這裏是我的代碼:

frag = new GalleryFragment(); 
frag.setImagePath(imagePath); 

FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
tx.replace(R.id.fragment_space, frag); 
tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 

和我的佈局文件:

<FrameLayout 
     android:id="@+id/fragment_space" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="65"/> 

回答

相關問題