片段 – 指定的孩子已经有一个父母。 您必须首先调用子对象的父对象的removeView()

我得到这个错误。 我尝试了许多解决scheme,但我coudnt解决这个问题。 帮我! 我需要将表面视图和button添加到使用片段的活动中。

CamActivity.java:

public class CamActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cam); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if(fragment == null) { fragment = new CamFragment(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } } 

CamFragment.java:

 public class CamFragment extends Fragment { private static final String TAG = "CamFragment"; private Camera mCamera; private SurfaceView mSurfaceView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){ View v = inflater.inflate(R.layout.camera_fragment, parent); Button capturePic = (Button)v.findViewById(R.id.img_capture); capturePic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getActivity().finish(); } }); mSurfaceView = (SurfaceView)v.findViewById(R.id.surfaceView1); return v; } } 

错误:

 04-18 13:24:12.735: E/AndroidRuntime(6321): FATAL EXCEPTION: main 04-18 13:24:12.735: E/AndroidRuntime(6321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pack.camdictionary/com.pack.camdictionary.CamActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1728) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1747) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.ActivityThread.access$1500(ActivityThread.java:155) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.os.Handler.dispatchMessage(Handler.java:130) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.os.Looper.loop(SourceFile:351) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.ActivityThread.main(ActivityThread.java:3814) 04-18 13:24:12.735: E/AndroidRuntime(6321): at java.lang.reflect.Method.invokeNative(Native Method) 04-18 13:24:12.735: E/AndroidRuntime(6321): at java.lang.reflect.Method.invoke(Method.java:538) 04-18 13:24:12.735: E/AndroidRuntime(6321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 04-18 13:24:12.735: E/AndroidRuntime(6321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659) 04-18 13:24:12.735: E/AndroidRuntime(6321): at dalvik.system.NativeStart.main(Native Method) 04-18 13:24:12.735: E/AndroidRuntime(6321): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.view.ViewGroup.addViewInner(ViewGroup.java:2007) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.view.ViewGroup.addView(ViewGroup.java:1902) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.view.ViewGroup.addView(ViewGroup.java:1859) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.view.ViewGroup.addView(ViewGroup.java:1839) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1166) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.Activity.performStart(Activity.java:3837) 04-18 13:24:12.735: E/AndroidRuntime(6321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701) 04-18 13:24:12.735: E/AndroidRuntime(6321): ... 11 more 

尝试更换

 View v = inflater.inflate(R.layout.camera_fragment, parent); 

 View v = inflater.inflate(R.layout.camera_fragment, parent, false); 

要么

 View v = inflater.inflate(R.layout.camera_fragment, null); 

这个问题已经回答了,但我仍然想添加作为第三个参数添加false的原因。

inflate()方法有三个参数:

  • 您要膨胀的布局的资源ID。
  • ViewGroup是充气布局的父级。 传递容器非常重要,以便系统将布局参数应用到膨胀布局的根视图,由其父视图指定。
  • 一个布尔值,指示膨胀期间是否应该将膨胀的布局附加到ViewGroup(第二个参数)。 (在这种情况下,这是错误的,因为系统已经将膨胀的布局插入容器传递true将在最终布局中创build冗余视图组。

资料来源: http : //developer.android.com/guide/components/fragments.html

为父和子元素指定Id ,特别是对于自定义视图,在xml文件中可能会解决问题。

尝试这个。

  ((CamActivity)getActivity()).finish();