我有一个片段VideoListPagerFragment
。这是来自YoutubePlayer API Android示例的VideoListDemoActivity。我从该示例活动中制作了片段,但我需要修复一个错误。
我将片段初始化为:
代码:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.videos_fragmentpage,container,true);
someAnotherMethods();
returt view;
}
FRAGMENT XML(R.layout.videos_fragmentpage):
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
>
<fragment
class="com.alexpoltavets.fifacommunity.videos.VideoListPagerFragment$VideoListFragment"
android:id="@+id/list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/video_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<ImageButton
android:id="@+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@android:drawable/btn_dialog"
android:onClick="onClickClose"/>
<fragment
class="com.alexpoltavets.fifacommunity.videos.VideoListPagerFragment$VideoFragment"
android:id="@+id/video_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</merge>
但是,当我开始我的活动: 添加片段的代码:
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.videos_container,VideoListPagerFragment.newInstance(),"test")
.commit();
活动XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/videos_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
我有一个错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexpoltavets.fifacommunity/com.alexpoltavets.fifacommunity.videos.VideosActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
如果我改变:
View view=inflater.inflate(R.layout.videos_fragmentpage,container,true);
为:
View view=inflater.inflate(R.layout.videos_fragmentpage,container,false);
我将:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexpoltavets.fifacommunity/com.alexpoltavets.fifacommunity.videos.VideosActivity}: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
如果我将<marge>
更改为<FrameLayout>
之类的其他容器,则会修复此错误,但是当屏幕旋转为横向时,我在另一个屏幕上有另外两个类似片段的错误。
我如何解决它?