问题
我正在Fragment中显示BottomSheetDialogFragment,它按预期工作。但是,当我在对话框中添加ViewPager并尝试添加适配器时,出现错误:
:host {
display: block
}
令人困惑的部分是,当我尝试设置适配器(或者可能使用findViewById())时,ViewPager通常会膨胀,但会导致应用程序崩溃
代码
BottomSheetDialogFragment类
No view found for id 0x7f0800f6 (my.package.name:id/viewPager) for fragment ChooseSenderFragment{dfc62a8 #2 id=0x7f0800f6 android:switcher:2131230966:0}
在片段中显示BottomSheetDialogFragment
public class ChooseBottomSheetDialogFragment extends BottomSheetDialogFragment {
public static ChooseBottomSheetDialogFragment newInstance() {
return new ChooseBottomSheetDialogFragment();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bottom_sheet_home_choose, container, false);
ViewPager viewPager = rootView.findViewById(R.id.viewPager);
viewPager.setAdapter(new ChooseAdapter(getFragmentManager()));
return rootView;
}
class ChooseAdapter extends FragmentPagerAdapter {
public ChooseAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return ChooseSenderFragment.newInstance();
case 1:
return ChooseSendingMethodFragment.newInstance();
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
}
}