我看到很多与此相关的帖子但仍然无法解决我的问题。我不知道我做错了什么。
我想使用片段为平板电脑实现双窗格UI。在手机和onClick的情况下,正常的单一窗格应该带我到第二个片段。
所以,当我给第二个片段充气时,我得到了这个例外。
这是我的堆栈跟踪,
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
at android.view.ViewGroup.addView(ViewGroup.java:4768)
at android.view.ViewGroup.addView(ViewGroup.java:4708)
at android.view.ViewGroup.addView(ViewGroup.java:4681)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1425)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是我的第二个片段类
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
if (savedInstanceState != null) {
steps = savedInstanceState.getParcelableArrayList("recipe_step_selected");
clickedIndex = savedInstanceState.getInt("clicked_index");
} else {
steps = getArguments().getParcelableArrayList("recipe_step_selected");
if (steps != null) {
steps = getArguments().getParcelableArrayList("recipe_step_selected");
clickedIndex = getArguments().getInt("clicked_index");
} else {
recipes = getArguments().getParcelableArrayList(getString(R.string.parcel_recipe));
steps = (ArrayList<Steps>) recipes.get(0).getSteps();
clickedIndex = 0;
}
}
View rootView = layoutInflater.inflate(R.layout.fragment_recipe_steps_snap, viewGroup, false);
simpleExoPlayerView = rootView.findViewById(R.id.playerView);
initializePlayer(Uri.parse(steps.get(clickedIndex).getVideoURL()));
initializeMediaSession();
recipeVideoInstruction = rootView.findViewById(R.id.recipe_video_instruction);
recipeVideoInstruction.setText(getArguments().getString(getResources().getString(R.string.recipe_video_step_instruction)));
return viewGroup;
}
这是片段的布局文件
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<android.support.constraint.Guideline
android:id="@+id/horizontalHalf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="256dp" />
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/playerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="0dp"
android:padding="0dp"
app:layout_constraintBottom_toTopOf="@+id/horizontalHalf"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:resize_mode="fill" />
<TextView
android:id="@+id/recipe_video_instruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:padding="16dp"
android:textSize="20sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/horizontalHalf" />