I have a problem and I cannot solve it. In my application, I have a navigation box, as shown below:
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> <fragment android:id="@+id/navigation_drawer" android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" />
In the MainActivity class, I usually set up aContentView by passing this xml: MainActivity.java
setContentView(R.layout.activity_main); mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
In my NavigationDrawerFragment onCreateView, I usually inflate its xml as follows:
NavigationDrawerFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = (View)inflater.inflate(R.layout.fragment_navigation_drawer,null,false); mDrawerListView = (ListView)root.findViewById(R.id.drawer_list_view); mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { selectItem(position); } });
And this is fragment_navigation_drawer.xml file: fragment_navigation_drawer.xml
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <ImageView android:id="@+id/profile_picture" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true"/> <TextView android:id="@+id/user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/profile_picture" android:textStyle="bold" android:textSize="15sp" /> <TextView android:id="@+id/user_mail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/user_name" android:layout_alignLeft="@id/user_name" android:fontFamily="sans-serif-light" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@android:drawable/ic_menu_close_clear_cancel"/> </RelativeLayout> <ListView 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:choiceMode="singleChoice" android:id="@+id/drawer_list_view" android:dividerHeight="0dp" android:background="@color/navigation_drawer_background" />
When I run the code, I get an exception in setContentView MainActivity saying:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child parent first.
I think something is wrong with the NavigationDrawer layout. What am I doing wrong?
EDIT: I changed my layout to:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/left_drawer_frame" android:layout_width="@dimen/navigation_drawer_dimension" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white"/>
So, using FrameLayout in my work, I use:
mFragment = new Fragment(); getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, mFragment) .commit();
And I inflate the fragment as follows:
View root = inflater.inflate(R.layout.fragment_main, container, false);
Everything seems to work