I have 3 fragment, first mainFragment
, tab1Fragment
, and Detaitab1Fragment
.
I want to call a Detaitab1Fragment in my tab1Fragment.
This is code when i call my Detaitab1Fragment in tab1Fragment
BlankFragment fragment = new BlankFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.commit();
and this code my SectionPageAdapter
on mainFragment
private void setupViewPager(ViewPager viewPager){
NotifikasiFragment.SectionPageAdapter adapter = new NotifikasiFragment.SectionPageAdapter(getChildFragmentManager());
adapter.addFragment(new TabKelasNotifikasiFragment(),"Kelas");
adapter.addFragment(new TabSekolahNotifikasiFragment(),"Sekolah");
viewPager.setAdapter(adapter);
}
public class SectionPageAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mfragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mfragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mfragmentList.get(position);
}
@Override
public int getCount() {
return mfragmentList.size();
}
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
and this is code when i call viewPager on mainFragment
SectionPageAdapter sectionPageAdapter = new SectionPageAdapter(getChildFragmentManager());
viewPager = (ViewPager) rootView.findViewById(R.id.containerNotifikasi);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabsNotifikasi);
tabLayout.setupWithViewPager(viewPager);
this code on below is my mainFragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fania.suara_project.fragment.notifikasi.NotifikasiFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#000000"
android:text="NOTIFIKASI"
android:textSize="20dp"
android:layout_below="@+id/text_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></TextView>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_below="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="@+id/tabsNotifikasi"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/containerNotifikasi"
android:layout_below="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
and code tab1Fragment.xml like this:
<RelativeLayout 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"
tools:context="com.example.fania.suara_project.fragment.notifikasi.TabKelasNotifikasiFragment">
<android.support.v7.widget.RecyclerView
android:background="@color/kuning"
android:id="@+id/recycleViewKelasNotifikasi"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
My FrameLayout is in content_drawable.xml
<RelativeLayout
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.fania.suara_project.activity.DrawableActivity"
tools:showIn="@layout/app_bar_drawable">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>
and i call content_drawable on Drawableactivity.class
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
R.id.drawable_layout is include content_drawable.xml
but i found error like this
java.lang.IllegalArgumentException: No view found for id 0x7f0e0099 (com.example.fania.suara_project:id/frame) for fragment BlankFragment{9df0d4d #0 id=0x7f0e0099}
anyone can help me ? Thank's
As far as I understand, your fragments are in the same activity, right? Try call getActivity().getFragmentManager()
(or getActivity().getSupportFragmentManager()
) instead getChildFragmentManager()
.
Your container with id="frame" is in your activity's xml?