Android 个片段 - 指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()
Android fragments - The specified child already has a parent. You must call removeView() on the child's parent first
我正在尝试创建一个带有导航抽屉的应用程序。在我尝试在我的导航抽屉片段中的列表视图上方添加另一个 TextView 之前,它运行良好。然后当我尝试在我的 HomeActivity 中设置 ContentView 时它给出了这个异常:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
以下是所有相关代码:
fragment_navigation_drawer.xml:(fb_name textview 是我需要添加的。)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/fb_name"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="60dp"
android:text="First Last"
android:textColor="@android:color/white" />
<ListView
android:id="@+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:choiceMode="singleChoice"
android:divider="#ffcdcdcd"
android:paddingTop="40dp"
tools:context=".NavigationDrawerFragment" />
</LinearLayout>
这是我膨胀片段的地方-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup group = (ViewGroup) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView = (ListView) group.findViewById(R.id.nav_drawer);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
ArrayList<DrawerItem> drawerItems = new ArrayList<DrawerItem>();
DrawerItem drawerItem1 = new DrawerItem("Home", R.drawable.home);
DrawerItem drawerItem2 = new DrawerItem("Start a Contest", R.drawable.hand_point);
DrawerItem drawerItem3 = new DrawerItem("All Contests", R.drawable.briefcase);
DrawerItem drawerItem4 = new DrawerItem("My Contests", R.drawable.binoculars);
DrawerItem drawerItem5 = new DrawerItem("Login", R.drawable.home);
drawerItems.add(drawerItem1);
drawerItems.add(drawerItem2);
drawerItems.add(drawerItem3);
drawerItems.add(drawerItem4);
drawerItems.add(drawerItem5);
DrawerAdapter adapter = new DrawerAdapter(getActivity(), drawerItems);
mDrawerListView.setAdapter(adapter);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
这是 setContentView 抛出异常的 activity onCreate() -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); //This is where the exception occurs
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
这是我的 DrawerLayout 所在的位置 -
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.labs.thinkdifferent.voice.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
我会试一试并建议你得到错误,因为你在 onCreateView()
而不是 group
中 return mDrawerListView
。来自 onCreateView()
视图的 returned 本身附加到另一个视图层次结构,在这种情况下这是不可能的,因为从 XML 布局它已经有一个父级 ViewGroup
.
我看不出有什么可以阻止在 onCreateView()
中 returning group
。
我正在尝试创建一个带有导航抽屉的应用程序。在我尝试在我的导航抽屉片段中的列表视图上方添加另一个 TextView 之前,它运行良好。然后当我尝试在我的 HomeActivity 中设置 ContentView 时它给出了这个异常:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
以下是所有相关代码:
fragment_navigation_drawer.xml:(fb_name textview 是我需要添加的。)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/fb_name"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="60dp"
android:text="First Last"
android:textColor="@android:color/white" />
<ListView
android:id="@+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:choiceMode="singleChoice"
android:divider="#ffcdcdcd"
android:paddingTop="40dp"
tools:context=".NavigationDrawerFragment" />
</LinearLayout>
这是我膨胀片段的地方-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup group = (ViewGroup) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView = (ListView) group.findViewById(R.id.nav_drawer);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
ArrayList<DrawerItem> drawerItems = new ArrayList<DrawerItem>();
DrawerItem drawerItem1 = new DrawerItem("Home", R.drawable.home);
DrawerItem drawerItem2 = new DrawerItem("Start a Contest", R.drawable.hand_point);
DrawerItem drawerItem3 = new DrawerItem("All Contests", R.drawable.briefcase);
DrawerItem drawerItem4 = new DrawerItem("My Contests", R.drawable.binoculars);
DrawerItem drawerItem5 = new DrawerItem("Login", R.drawable.home);
drawerItems.add(drawerItem1);
drawerItems.add(drawerItem2);
drawerItems.add(drawerItem3);
drawerItems.add(drawerItem4);
drawerItems.add(drawerItem5);
DrawerAdapter adapter = new DrawerAdapter(getActivity(), drawerItems);
mDrawerListView.setAdapter(adapter);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
这是 setContentView 抛出异常的 activity onCreate() -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); //This is where the exception occurs
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
这是我的 DrawerLayout 所在的位置 -
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.labs.thinkdifferent.voice.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
我会试一试并建议你得到错误,因为你在 onCreateView()
而不是 group
中 return mDrawerListView
。来自 onCreateView()
视图的 returned 本身附加到另一个视图层次结构,在这种情况下这是不可能的,因为从 XML 布局它已经有一个父级 ViewGroup
.
我看不出有什么可以阻止在 onCreateView()
中 returning group
。