找不到 ID viewpager 片段的视图 android
no view found for id viewpager fragment android
我有 3 个片段,第一个 mainFragment
、tab1Fragment
和 Detaitab1Fragment
。
我想在我的 tab1Fragment.
中调用 Detaitab1Fragment
这是我在 tab1Fragment 中调用 Detaitab1Fragment 时的代码
BlankFragment fragment = new BlankFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.commit();
而这段代码是我在 mainFragment
上的 SectionPageAdapter
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);
}
}
这是我在 mainFragment 上调用 viewPager 时的代码
SectionPageAdapter sectionPageAdapter = new SectionPageAdapter(getChildFragmentManager());
viewPager = (ViewPager) rootView.findViewById(R.id.containerNotifikasi);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabsNotifikasi);
tabLayout.setupWithViewPager(viewPager);
下面的代码是我的 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>
和代码 tab1Fragment.xml 如下:
<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>
我的 FrameLayout 在 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>
我在 Drawableactivity.class
上给 content_drawable 打电话
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 包含 content_drawable.xml
但是我发现了这样的错误
java.lang.IllegalArgumentException: No view found for id 0x7f0e0099 (com.example.fania.suara_project:id/frame) for fragment BlankFragment{9df0d4d #0 id=0x7f0e0099}
谁能帮帮我?
谢谢
据我了解,您的片段在同一个 activity 中,对吗?尝试调用 getActivity().getFragmentManager()
(或 getActivity().getSupportFragmentManager()
)而不是 getChildFragmentManager()
。
您的 id="frame" 容器在您的 activity 的 xml?
中
我有 3 个片段,第一个 mainFragment
、tab1Fragment
和 Detaitab1Fragment
。
我想在我的 tab1Fragment.
这是我在 tab1Fragment 中调用 Detaitab1Fragment 时的代码
BlankFragment fragment = new BlankFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame, fragment);
transaction.commit();
而这段代码是我在 mainFragment
上的SectionPageAdapter
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);
}
}
这是我在 mainFragment 上调用 viewPager 时的代码
SectionPageAdapter sectionPageAdapter = new SectionPageAdapter(getChildFragmentManager());
viewPager = (ViewPager) rootView.findViewById(R.id.containerNotifikasi);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabsNotifikasi);
tabLayout.setupWithViewPager(viewPager);
下面的代码是我的 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>
和代码 tab1Fragment.xml 如下:
<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>
我的 FrameLayout 在 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>
我在 Drawableactivity.class
上给 content_drawable 打电话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 包含 content_drawable.xml
但是我发现了这样的错误
java.lang.IllegalArgumentException: No view found for id 0x7f0e0099 (com.example.fania.suara_project:id/frame) for fragment BlankFragment{9df0d4d #0 id=0x7f0e0099}
谁能帮帮我? 谢谢
据我了解,您的片段在同一个 activity 中,对吗?尝试调用 getActivity().getFragmentManager()
(或 getActivity().getSupportFragmentManager()
)而不是 getChildFragmentManager()
。
您的 id="frame" 容器在您的 activity 的 xml?