从 Android 图库中选择照片然后将其显示在片段中?

发布于 2024-12-20 17:37:04 字数 4389 浏览 2 评论 0原文

我正在开发一个使用片段的 Android 应用程序,并希望用户能够选择图像进行扫描。我有一个片段,允许用户选择他们想要的来源(相机、手绘或画廊)。当用户选择图库时,另一个片段应该显示该图像。我已经知道应该如何将图像放入 ImageView 中,但在 Fragments 中不断出现此错误:

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"/>

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 ImageView but keep getting this error with the Fragments:

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

Basically it looks like there's an issue with my FragmentTransaction usage. Here's my code:

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

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

and my layout file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文