无法在BottomSheetDialogFragment中设置ViewPager适配器

时间:2019-01-27 11:08:31

标签: java android android-fragments android-viewpager bottom-sheet

问题

我正在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;
    }
}

}

0 个答案:

没有答案