Android 如何将BottomSheetBehavior与BottomSheetDialogFragment集成

Android 如何将BottomSheetBehavior与BottomSheetDialogFragment集成,android,android-layout,android-fragments,bottom-sheet,Android,Android Layout,Android Fragments,Bottom Sheet,我尝试在BottomSheetDialogFragment中获取一个行为,因此我会使其不可撤销,但它会引发以下错误: 原因:java.lang.NullPointerException:尝试调用虚拟机 方法“android.view.ViewGroup$LayoutParams” 空对象引用上的android.view.view.getLayoutParams() 你认为可能是什么? 你有什么想法或教程来解决这个问题吗。或者一些可能会教给我详细信息的东西 代码: public class Bot

我尝试在
BottomSheetDialogFragment
中获取一个行为,因此我会使其不可撤销,但它会引发以下错误:

原因:java.lang.NullPointerException:尝试调用虚拟机 方法“android.view.ViewGroup$LayoutParams” 空对象引用上的android.view.view.getLayoutParams()

你认为可能是什么? 你有什么想法或教程来解决这个问题吗。或者一些可能会教给我详细信息的东西

代码:

public class BottomSheetExcample extends BottomSheetDialogFragment {

private BottomSheetBehavior bottomSheetBehavior;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.bottom_sheet, container, false);

    view.findViewById(R.id.txt1);
    view.findViewById(R.id.txt2);

    bottomSheetBehavior = BottomSheetBehavior.from();


    return view;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
    View view = View.inflate(getContext(), R.layout.bottom_sheet, null);

    TextView txt1 = view.findViewById(R.id.txt1);
    TextView txt2 = view.findViewById(R.id.txt2);

    dialog.setContentView(view);
    bottomSheetBehavior = BottomSheetBehavior.from((View) view.getParent());
    return dialog;
}

@Override
public void onStart() {
    super.onStart();
    bottomSheetBehavior.setHideable(false);
    bottomSheetBehavior.setPeekHeight(500, true);
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}


删除onCreateView方法

    public class BottomSheetExample extends BottomSheetDialogFragment {

    private BottomSheetBehavior bottomSheetBehavior;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        View view = View.inflate(getContext(), R.layout.fragment_cart, null);


        dialog.setContentView(view);
        bottomSheetBehavior = BottomSheetBehavior.from((View) view.getParent());
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        bottomSheetBehavior.setHideable(false);
        bottomSheetBehavior.setPeekHeight(500);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

}
    public class BottomSheetExample extends BottomSheetDialogFragment {

    private BottomSheetBehavior bottomSheetBehavior;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
        View view = View.inflate(getContext(), R.layout.fragment_cart, null);


        dialog.setContentView(view);
        bottomSheetBehavior = BottomSheetBehavior.from((View) view.getParent());
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        bottomSheetBehavior.setHideable(false);
        bottomSheetBehavior.setPeekHeight(500);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

}