我正在使用带有片段的BottomNavigation,并且在我的一个底部片段中,我在片段中添加了片段并使用了tablayout,一切工作正常,但有时应用程序崩溃会产生以下错误。
java.lang.IllegalArgumentException: No view found for id 0x7f0904b3 (com.my.app:id/viewpagers) for fragment MyFavoriteFragment
这是我的底部导航
现在在第四个片段中,我使用viewpager进行了布局,以下是我的代码,任何人都可以帮我解决这个问题 父片段
public class CartAndFavouriteMainFragment extends BaseFragment {
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private TabLayout tabLayout;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public int getLayoutId() {
return R.layout.fragment_cart_fvrt;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
viewPager = (ViewPager) view.findViewById(R.id.viewpagers);
viewPager.setOffscreenPageLimit(2);
viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setToolTitle("Cart");
SharedPreferences prefs = getContext().getSharedPreferences("User_Details", MODE_PRIVATE);
android.support.v7.widget.Toolbar toolbar = view.findViewById(R.id.tool_bar);
if (prefs.getString("isReseller", "").equalsIgnoreCase("Y")) {
toolbar.setBackgroundColor(getResources().getColor(R.color.colorAccent));
} else {
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
tabLayout.setVisibility(View.GONE);
}
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new CartFragment();
} else if (position == 1) {
fragment = new MyFavoriteFragment();
}
getChildFragmentManager().executePendingTransactions();
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
String title = null;
if (position == 0) {
title = "My Cart";
} else if (position == 1) {
title = "My Favourites";
}
return title;
}
}
@Override
public void onResume() {
super.onResume();
}
}