• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Nested fragment inside a fragment in a DrawerLayout?

Ha Ha

Lurker
Dec 22, 2014
1
0
Hello,

This is my first time with Android so I apologize in advance for the newbie question. I have been trying to do nested fragment inside fragment, within the DrawerLayout without any luck. I tested the nested fragment without DrawerLayout, it works well. I tested the fragment layout without adding the nested fragment. It works well also.

I really appreciate any help! Thanks!

I am getting the error after adding the nested fragment.

12-22 19:01:21.877: E/AndroidRuntime(3891): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f050041 (com.hxcellent.checked:id/content_frame) for fragment NestedFrag{b314e510 #0 id=0x7f050041 left}

It is a simple layout:

DrawerLayout <- Created as the initial activity
LeftFrag <- Created as a fragment
NestedFrag <- nested Fragment
In the DrawerLayout activity, I replace content_frame with LeftFrag fragment when user select an item in the Menu:

fragmentTransaction = fragmentTransaction.replace(R.id.content_frame, new LeftFrag());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

In the LeftFrag fragment, I add the NestedFrag in the onCreateView:

@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.left_frag, container, false);
Fragment videoFragment = new NestedFrag();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack("left");
transaction.add(getId(), videoFragment, "left");
transaction.commit();
return root;​
}

<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" />


<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />​

</android.support.v4.widget.DrawerLayout>

public class NestedFrag extends Fragment {
public NestedFrag() {

}

@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.nested_frag, container, false);
return root;
}

private void destroyFragment() {
getChildFragmentManager().beginTransaction().hide(this).commit();
}
}

public class LeftFrag extends Fragment {
TextView changeableText;

@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// set the view
View root = inflater.inflate(R.layout.left_frag, container, false);
Fragment videoFragment = new NestedFrag();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack("left");
transaction.add(getId(), videoFragment, "left");
transaction.commit();
return root;​
}
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones